Bindings
Read-only bindings
Section titled “Read-only bindings”let creates a read-only binding. let disallows writes to the value and reassignment of the name.
let defaults = [10, 20, 30]let timeout = 30
defaults[0] = 60error: write not allowedtimeout = 60error: reassignment not allowedMutable bindings
Section titled “Mutable bindings”let mut creates a mutable binding. let mut allows writes to the value and reassignment of the name.
let mut scores = [90, 85, 77]let mut count = 0
scores[0] = 100write allowedcount += 1reassignment allowedWrite permission in bindings
Section titled “Write permission in bindings”In Go, []T, map[K]V and *T allow two names to share the same data, so a write through one can change what the other sees. Lisette makes that risk visible in the type: let mut encodes write permission in the value’s type.
Slice, Map and Ref are writable only when the type carries mut.
let mut scores = [90, 85, 77]mut Slice<int>scores[0] = 100write allowed
let ranks = [1, 2, 3]Slice<int>ranks[0] = 5error: write not allowedSee also Write permission in parameters and Write permission in fields
A name that shares data cannot have more write permission than the original.
let a = [1, 2, 3]read-onlylet mut b = areassignment allowed, but read-only like ab[0] = 99error: b is read-only
let mut c = a.clone()independent copyc[0] = 99write allowed, a unchangedConstant bindings
Section titled “Constant bindings”const defines a compile-time constant. Its type must be numeric, bool, or string, and its initializer can use only literals and other constants. A constant has no address.
const MAX_SIZE = 1024const GREETING = "hello"const DOUBLED = MAX_SIZE * 2A tuple, struct, Slice, or Array cannot be const. Use a function that returns the value instead:
fn origin() -> Point { Point { x: 0, y: 0 }}Annotating a binding
Section titled “Annotating a binding”Annotate a type with : after the binding name. The annotation is optional, because the type usually follows from the value. A function parameter always needs one.
let x: int = 42let y = 42Destructuring a binding
Section titled “Destructuring a binding”A binding can take a value apart with a pattern. Each name in the pattern binds to the part in its position.
let t = (10, 20)let (a, b) = ta is 10, b is 20
let p = Point { x: 3, y: 4 }let Point { x, y } = px is 3, y is 4