Ownership
Vex uses ownership to make the lifetime and movement of values explicit. An ordinary owning value has an owner; a managed allocation may be kept alive by multiple explicit handles. Passing an owning value to a function or assigning it to another binding can move that ownership; borrowing lets code use the value without taking it over.
Container and resource APIs build on these rules: they specify which operations borrow, clone, share, transfer, or destroy a value. Foreign types also retain the foreign library's ownership contract.
Complete programs on this page print their computed values and return 0 for successful execution. Printed output and process exit status are separate: returning 42 from main(): i32 would report a nonzero process status, not print the number. Fragments without main belong inside a program.
Bindings and mutation
Bindings are immutable by default:
fn main(): i32 {
let value = 20;
$println(value);
return 0;
}Use let! when the binding must be reassigned:
fn main(): i32 {
let! total = 20;
total += 22;
$println(total);
return 0;
}A mutable binding does not make every reference to the value mutable. The reference type must also permit mutation.
Moves
Owning values such as Vec, Box, strings, maps, sets, and most user-defined structs may be moved:
let data = Vec.new<i32>()
let moved = data
use(moved)
use(data) // error: data was movedA move is useful when a function or task should become the new owner. If the caller still needs to use the value, pass an immutable or mutable reference instead.
Copy values
Small scalar values such as integers, floating-point values, booleans, and characters are copied in ordinary assignments:
fn main(): i32 {
let first: i32 = 42;
let second = first;
$println(first);
$println(second);
return 0;
}For an owning or user-defined type, assume that assignment moves unless the type's contract says otherwise.
Borrowing
An immutable borrow uses &T:
fn inspect(value: &Point): i32 {
return value.x + value.y
}A mutable borrow uses &T!:
fn update(value: &Point!) {
value.x = 10
}There may be several immutable borrows, or one exclusive mutable borrow, but the two kinds cannot conflict while they are live. The checker uses non-lexical lifetime reasoning, so an immutable borrow can end at its last use rather than at the end of the surrounding block.
Partial moves
Ownership is tracked at the field level for supported structs. Moving one non-Copy field does not automatically move every independent field:
struct Pair {
public:
name: string,
age: i32,
}
let pair = Pair { name: "Alice", age: 30 }
let name = pair.name
use(pair.age)The moved field cannot be read again. Copy fields can remain available when the surrounding type and access path permit it.
References returned from functions
A returned reference must be derived from data that outlives the returned reference:
fn longest(left: &string, right: &string): &string {
if left.len() > right.len() {
return left
}
return right
}Returning a reference to a local owned value is rejected because the local is destroyed when the function returns.
Heap-owned values
Box is an owning heap value. It is useful when a value needs indirection or a recursive data structure needs a stable allocation:
let boxed = Box.new(42)
use(boxed)Box does not bypass ownership. Moving, borrowing, and dropping a boxed value still follow the same rules.
Low-level views
Use the narrowest memory abstraction that expresses the operation:
- &T and &T! for ordinary borrows;
Span<T>for a non-owning contiguous view;Ptr<T>for typed pointer operations;- RawBuf for explicit raw storage.
Keep raw memory and FFI code behind a small, tested boundary. See FFI, Pointers, and Memory Safety.
Practical rules
- Borrow when the callee should not become the owner.
- Move when ownership transfer is intentional.
- Make mutation visible with let! and mutable references.
- Use explicit types at FFI and ABI boundaries.
- Check detached-task captures carefully; a go block cannot safely outlive a borrowed local.
Next steps
- Ownership workflows — complete borrowed-parser and owned-report examples
- Borrowing
- Lifetimes
- Memory Safety
- VUMM