Variables

Data can be stored in variables using the let keyword.

Variables are immutable by default, but this can be overridden using the mut keyword.

Variables can be type annotated, but Cairo can also infer types from context.

Example

fn main() {
    let immutable_var: u8 = 17; // explicit type annotation
    // immutable_var = 38;  <-- fails to compile
    let mut mutable_var = immutable_var; // type u8 inferred from context
    mutable_var = 38;
    assert!(mutable_var != immutable_var);
}

You can experiment with this example in cairovm.codes and read more about variables in the Cairo Book.