Variables and Bindings
Explanation
Section titled “Explanation”Variables in Zeen are named data owners: they hold a type and a pointer to owned data.
Variables may be left unassigned at initialization, but they cannot be used before initialization.
By default locals in Zeen are mutable, you can use the const modifier to disable mutability.
Syntax:
Section titled “Syntax:”// mutablelet identifier: type;let identifier: type = value;let identifier = value;
// non-mutableconst identifier: type;const identifier: type = value;const identifier = value;Examples:
// Init without valuelet var: i32;
// Init with type and valuelet var: i32 = 123;
// Init with only valuelet var = 123;Globals
Section titled “Globals”Top-level globals need explicit type and value.
Syntax:
const NAME: type = value;let name: type = value;Example:
const MAX: i32 = 100;let counter: i32 = 0;Discard
Section titled “Discard”Use _ to discard value without drop:
let _ = foo();Detailed Example
Section titled “Detailed Example”A variable is a pointer to temporary stack-allocated data, existing in the current function context. It provides the means to load, change, or remove this data. When the function context ends, all existing data will be freed. Pointers are also stack-allocated values, more explanation about pointers you’ll find in the related topic.
Code:
fn main() { let a = 1234; let ptr = &a;}In simple representation will look like:
FN STACK ┌────────┐ ┌──────────────┐┌──►│ 1234 │◄────────┤ let a = 1234 ││ │────────│ └──────────────┘└───┤ 0xADDR │◄──────┐ │────────│ │ ┌──────────────┐ │ .... │ └─│ let ptr = &a │ │────────│ └──────────────┘ │ .... │ └────────┘