Variables
A variable binds a name to a value. Every binding is either immutable or mutable, chosen by the keyword that introduces it:
| Binding | Mutability | Description |
|---|---|---|
let | Immutable | Cannot be reassigned after initialization |
var | Mutable | Can be reassigned at any time |
rux
let answer = 42; // immutable
var counter = 0; // mutable
counter += 1; // OKImmutability is the default: prefer let, and reach for var only when a value genuinely needs to change.
Type Inference and Annotations
When a binding has an initializer, the compiler infers its type. Add an explicit : Type annotation to choose a type other than the inferred one — a wider integer, say — or to declare a variable whose type cannot be inferred:
rux
let pi = 3.14159; // inferred float64
let name: char8[] = "Rux"; // explicit annotation
var total: int64 = 0; // explicit — wider than the default intA variable's type is fixed once it is declared. Because Rux performs no implicit conversions, assigning a value of a different type is a compile-time error; convert explicitly with an as cast.
Topics in This Chapter
| Topic | Description |
|---|---|
Immutable Variables (let) | The default binding and why to prefer it |
Mutable Variables (var) | Reassignment and compound assignment |
| Mutability of Structs | How let and var govern field changes |
For named values computed at compile time, use const instead.
See Also
- Constants — compile-time named values
- Type Casts — converting between types when assigning