Skip to content

Assignments

Calibre gives you three common types of variables: immutable, mutable, and constant.

Use let for an immutable variable.

Immutable variables cannot be reassigned after they are created.

let name := "Calibre";
let age := 5;

Use let mut when the value needs to change.

Mutable variables can also be reassigned or edited whenever needed.

let mut count := 0;
count += 1;
count += 1;
let mut total := 10;
total := total + 5;

In Calibre, assignment is also an expression.

When you assign to a variable, the assignment expression evaluates to the previous value that variable held.

This can be useful when you want to update a value while still keeping track of what it was before the assignment.

Also note that this also applies to expressions like += or -=.

let mut total := 10;
let old := total := 25;
print(old); // 10
print(total); // 25

Use const for variables that should stay constant, such as shared helpers or top-level definitions (only const declarations are allowed at the top-level).

Calibre can often infer the type of a binding from the value on the right-hand side.

const max_users := 1000;
const add := fn (a b : int) -> int => a + b;
let score := 42;
let title := "Tour";
let ready := true;

When you want to be explicit, add a type annotation between the : and the =.

let score : int = 42;
let title : str = "Tour";
let ready : bool = true;

Type annotations also work with mutable and constant variables.

let mut total : int = 0;
const pi : float = 3.14159;

Type annotations are especially useful when code should be clearer and more explicit to readers or when a expression could be interpreted in different ways.