>

Mutable & Immutable

By default rust makes everything immutable, that is unchangable.

1
2
3
let x: i32 = 3;
println!("x = {}", x)
// x += 1; // This won't work, x isn't mutable/changable

It can become mutable/changable.

1
2
3
4
let mut x: i32 = 3;
println!("x = {}", x);
x += 1; // This works because x is mutable
println!("x = {}", x);

One can also make a immutable into a mutable using shadowing…

1
2
3
4
5
let x: i32 = 3;
println!("x = {}", x);
let mut x: i32 = 4; // This actually shadows x
x += 1; // This works because x is mutable
println!("x = {}", x);

2023-10-24