Skip to content

Variables and Bindings

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.

// mutable
let identifier: type;
let identifier: type = value;
let identifier = value;
// non-mutable
const identifier: type;
const identifier: type = value;
const identifier = value;

Examples:

// Init without value
let var: i32;
// Init with type and value
let var: i32 = 123;
// Init with only value
let var = 123;

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;

Use _ to discard value without drop:

let _ = foo();

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 │
│────────│ └──────────────┘
│ .... │
└────────┘