Variables

A variable binds a name to a value. Every binding is either immutable or mutable, chosen by the keyword that introduces it:

BindingMutabilityDescription
letImmutableCannot be reassigned after initialization
varMutableCan be reassigned at any time
let answer = 42;   // immutable
var counter = 0;   // mutable
counter += 1;      // OK

Immutability is the default: prefer let, and reach for var only when a value genuinely needs to change. Function parameters follow the same rule — immutable unless declared var (see Mutable Parameters).

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:

let pi = 3.14159;            // inferred float64
let name: Slice<char8> = "Rux";   // explicit annotation
var total: int64 = 0;        // explicit — wider than the default int

A 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

TopicDescription
Immutable Variables (let)The default binding and why to prefer it
Mutable Variables (var)Reassignment and compound assignment
Mutability of StructsHow let and var govern field changes

For named values computed at compile time, use const instead.

See Also