Shadowing and Constants

Shadowing in Rust allows you to declare a new variable with the same name as a previous variable, effectively replacing the original one.

Example

let x = 5;
let x = x + 1;
let x = x * 2;
println!("x is: {}", x);

Shadowing lets you reuse variable names without mutability, allowing transformations and keeping code clean.

Constants

Constants are values that never change and are always immutable. They must have a type annotation and can be declared in any scope.

const MAX_POINTS: u32 = 100_000;

Unlike variables, constants can’t be shadowed or defined using let.

← PrevNext →