Variables
Declaration
Variables are declared with their type, name, and an optional initializer:
i32 x = 10;
i64 big = 9999999;
i8* message = "hello";Immutability by Default
All variables are immutable by default. Attempting to reassign will produce a compile error:
i32 x = 10;
x = 20; // Error: cannot reassign immutable variableMutable Variables
Use the mut keyword after the variable name to allow reassignment:
i32 mut counter = 0;
counter = counter + 1; // OKMutable parameters use mut before the type:
void increment(mut i32 x) {
x = x + 1;
}Type Inference
Use auto to let the compiler infer the type from the initializer:
auto x = 42; // inferred as i32
auto p = Point(1, 2); // inferred as Pointauto works with mut:
auto mut result = 0;
result = compute();Compile-Time Constants
Use constexpr to declare variables evaluated entirely at compile time:
constexpr i32 MAX_SIZE = 1024;
constexpr i32 BUFFER = 4 * MAX_SIZE;constexpr variables support arithmetic, comparison, logic, and references to other constants. They can be used anywhere a constant is expected, including fixed-size array lengths:
constexpr i32 SIZE = 64;
i8* buf = i8[SIZE]; // stack-allocated array of 64 bytesSee Compile-Time Evaluation for full details on constexpr and consteval.
Scope
Variables are block-scoped. Inner blocks can shadow outer variables:
i32 main() {
i32 x = 10;
{
i32 x = 20; // shadows outer x
}
return x; // 10
}