Skip to content

Mutable vs immutable variables

Course: Rust for Solana contracts - chapter 7

Ref - ‣

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}");
}