Variables and Mutability
As mentioned in the “Storing Values with Variables” section, by default, variables are immutable.
Variables
Filename: src/main.rs
fn main() {
let mut x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
let x = 7;
println!("The value of x is: {x}");
}
Declaring constants
Filename: src/main.rs
#![allow(unused)]
fn main() {
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
}
Shadowing
Filename: src/main.rs
fn main() {
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in the inner scope is: {x}");
}
println!("The value of x is: {x}");
}