Variables in Rust
Variables are used to store data in memory.
In Rust, variables are immutable by default, which means their values cannot be changed after creation.
Video Explanationâ

Declaring a Variableâ
fn main() {
let x = 10;
println!("{}", x);
}
Explanationâ
letis used to declare a variable.xis the variable name.10is the value stored in the variable.println!()prints the value.
Immutable Variablesâ
fn main() {
let x = 5;
// x = 6; â Error
}
Rust does not allow changing immutable variables.
Mutable Variablesâ
To make a variable changeable, use mut.
fn main() {
let mut x = 5;
x = 10;
println!("{}", x);
}
Explanationâ
mutmeans mutable.- Mutable variables can change their values.
Variable Shadowingâ
Rust allows redeclaring variables using the same name.
fn main() {
let x = 5;
let x = x + 1;
println!("{}", x);
}
Outputâ
6
Shadowing creates a new variable with the same name.
Track Your Progress
Done with this topic? Mark it as complete to track your progress.
đŦ Discuss this page
Have a question or spot something confusing in "Variables in Rust"? Ask below â it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.