Variables and Mutability in Rust
In this lesson, you will learn how variables work in Rust and how Rust handles mutability by default.
Rust takes a unique and strict approach to variables in order to ensure memory safety and prevent unexpected bugs.
What Is a Variable?
A variable is a named storage location used to hold data.
In Rust, variables allow you to store values that can be used later in a program. However, Rust variables behave differently compared to many other programming languages.
Declaring Variables Using let
In Rust, variables are declared using the let keyword.
Example:
fn main() {
let x = 10;
println!("{}", x);
}
Here, the variable x stores the value 10.
Immutability by Default
By default, variables in Rust are immutable. This means once a value is assigned, it cannot be changed.
Example:
fn main() {
let x = 5;
x = 10; // Error
}
This code will produce a compilation error because Rust does not allow changing an immutable variable.
This design helps prevent accidental changes and improves program safety.
Making Variables Mutable
To allow a variable to be changed, you must explicitly mark it as mutable
using the mut keyword.
Example:
fn main() {
let mut x = 5;
println!("{}", x);
x = 10;
println!("{}", x);
}
Here, the variable x is mutable, so its value can be updated.
Why Rust Uses Immutability
Rust encourages immutability because it:
- Prevents unexpected changes
- Makes code easier to reason about
- Improves safety in concurrent programs
- Reduces bugs caused by shared state
Mutability is allowed, but only when explicitly requested.
Variable Shadowing
Rust allows you to declare a new variable with the same name as an existing one. This is called shadowing.
Example:
fn main() {
let x = 5;
let x = x + 1;
let x = x * 2;
println!("{}", x);
}
Each let creates a new variable that shadows the previous one.
Shadowing is different from mutability because it creates a new variable instead of modifying an existing one.
Shadowing vs Mutability
Key differences between shadowing and mutability:
- Shadowing creates a new variable
- Mutability changes the same variable
- Shadowing can change the variable type
- Mutability cannot change the type
Example of changing type using shadowing:
fn main() {
let x = "hello";
let x = x.len();
println!("{}", x);
}
Variable Scope
A variable is only accessible within the block where it is defined.
Example:
fn main() {
let x = 10;
{
let y = 20;
println!("{}", y);
}
println!("{}", x);
}
The variable y is only available inside the inner block.
📝 Practice Exercises
Exercise 1
Create an immutable variable and print its value.
Exercise 2
Create a mutable variable, update its value, and print it.
Exercise 3
Use variable shadowing to change a variable’s value.
Exercise 4
Use shadowing to change a variable’s data type.
✅ Practice Answers
Answer 1
fn main() {
let x = 100;
println!("{}", x);
}
Answer 2
fn main() {
let mut x = 5;
x = 15;
println!("{}", x);
}
Answer 3
fn main() {
let x = 3;
let x = x + 2;
println!("{}", x);
}
Answer 4
fn main() {
let value = "Rust";
let value = value.len();
println!("{}", value);
}
What’s Next?
Now that you understand variables and mutability, you are ready to learn about data types in Rust.
In the next lesson, you will explore Rust’s scalar and compound data types in detail.