References
Ref<T> is a reference to a value of type T, equivalent to a Go pointer but guaranteed non-nil.
Referencing
Section titled “Referencing”Use & to take a reference to anything that has an address, such as a variable, a field, or an element. Literals, map values, and const bindings have none.
let user = User { name: "Alice", tags: ["admin"] }
let user_ref = &userRef<User> pointing to userlet name_ref = &user.nameRef<string> pointing to user.namelet tag_ref = &user.tags[0]Ref<string> pointing to user.tags[0]Dereferencing
Section titled “Dereferencing”Use .* to follow a reference back to the pointee:
let name = name_ref.*"Alice"let tag = tag_ref.*"admin"Mutation
Section titled “Mutation”Only a mut Ref<T> can be written to:
fn bump(n: mut Ref<int>) { n.* += 1}
let mut count = 1bump(&count)&count is a mut Ref<int>
let total = 1bump(&total)error: expected mut Ref<int>, found Ref<int>Refs only grant what the place they point to allows. &x is a mut Ref<T> if x allows writes, else a plain Ref<T>.
Implicit & and .* in method calls
Section titled “Implicit & and .* in method calls”When calling methods, Lisette auto-adds & or .* as needed.
struct Rectangle { width: float64, height: float64,}
impl Rectangle { fn area(self) -> float64 { self.width * self.height }
fn scale(self: mut Ref<Rectangle>, factor: float64) { self.width *= factor }}
let mut rect = Rectangle { width: 10.0, height: 5.0 }let shape = &rect
let a = shape.area()equivalent to shape.*.area()rect.scale(2.0)equivalent to (&rect).scale(2.0)