Skip to content

Mutable vs immutable variables

Ref - https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html

By default, all variables in rust are imutable.

The following code will not compile -

fn main() {
let x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x isa {x}");
}

The following code will compile -

fn main() {
let mut x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x isa {x}");
}