Tsallaka zuwa abun ciki
Justice Tech School

Mutability and Shadowing

let makes an immutable binding and let mut allows changes. Shadowing declares a new variable with the same name, even with a new type.

Example
let x = 5;
let x = x + 1;
let x = format!("value {}", x);
println!("{}", x);
let mut count = 0;
count += 1;
println!("{}", count);

Result

value 6
1

Exercise

Allow a change.

let  n = 0;

Quiz Test

What does shadowing do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.