Immutable Variables (let)
A variable introduced with let is immutable: once initialized, it cannot be reassigned. Immutability is the default in Rux — reach for let first and switch to var only when mutation is genuinely needed.
let x = 42; // type inferred as int
let name: Slice<char8> = "Rux"; // explicit type annotation
x = 10; // error: cannot assign to an immutable binding
Why Prefer let
Defaulting to let makes the few values that do change stand out, and it lets the compiler reject accidental writes. A binding you never intend to reassign should say so, so that readers — and the compiler — can rely on it.
Immutability Extends to Fields
For a value type such as a struct, let makes the whole value immutable, not just the name. You can neither reassign the variable nor modify its fields:
let p = Point { x: 1.0, y: 2.0 };
p = Point { x: 0.0, y: 0.0 }; // error: cannot reassign a let binding
p.x = 5.0; // error: cannot modify a field of an immutable struct
See Mutability of Structs for the complete rules.
See Also
- Mutable Variables (
var) — when a value needs to change - Mutability of Structs — how
letpropagates into fields - Constants — immutable values known at compile time
Overview
A variable binds a name to a value. Every binding is either immutable or mutable, chosen by the keyword that introduces it:
Mutable
A variable introduced with var is mutable: it can be reassigned at any time after declaration. Use it for values that genuinely change — counters, accumulators, loop state — and prefer let for everything else.