Skip to content

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 allowed
timeout = 60error: reassignment not allowed

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 allowed
count += 1reassignment allowed

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 allowed

See 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-only
let mut b = areassignment allowed, but read-only like a
b[0] = 99error: b is read-only
let mut c = a.clone()independent copy
c[0] = 99write allowed, a unchanged

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 = 1024
const GREETING = "hello"
const DOUBLED = MAX_SIZE * 2

A tuple, struct, Slice, or Array cannot be const. Use a function that returns the value instead:

fn origin() -> Point {
Point { x: 0, y: 0 }
}

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 = 42
let y = 42

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