Properties2
| Type | Concept |
| Note created | Feb 17, 2025 |
In Rust, all variables are immutable by default. To be able to assign a new value to a variable, we need to explicitly make them mutable:
let mut x = 5;
x = 6;
println!("The value of x is: {x}");On the other hand, we have constants. Constants are not immutable by default - they are always immutable. To declare them, we need to use the const keyword:
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;In Rust, it is possible to shadow variables - simply declare a new variable using an existing name to shadow a within the scope. It is important to understand that:
- Shadowing is not mutability: we are not changing the value of the variable. It is a new one entirely.
- Shadowing allows to use a different data type (since it is a different variable).