Caro is thinking out loud

Created Last updated

Rust variables are immutable by default

When you declare a variable with let, the value can’t be changed later. If you try to change the value, the compiler will complain.

If you want a variable to be mutable, you have to declare it with let mut, then you can change its value (but not its type).

There are also constants, which you declare like const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3; . Constants have to be assigned to a constant expression (think the result of some maths or a string), not something that has to be computed at runtime. They have to have an explicit type annotation. You can declare them in any scope

You can “shadow” an immutable variable by declaring another variable with the same name, in the same or a different scope

fn main() {
    let x = 5;

    let x = x + 1;

    {
        let x = x * 2;
        println!("The value of x in the inner scope is: {x}");
        // prints 12, then that value goes out of scope
    }

    println!("The value of x is: {x}");
    // prints 6
}

Shadowing also allows you to change the type of an already declared variable, like this

fn main() {
    let spaces = "   ";  // this one is an &str
    let spaces = spaces.len();  // this one is a usize - i'm on a 64-bit machine so this is a 64 bit unsigned integer
}

You probably mainly use it for changing types via parsing in real life, for avoiding having a load of variables like x_number, x_string and the like. Or possibly for re-binding after having done some validation, or a pattern match or whatever.

Sources