Rust Variables

Rust Variables – Complete Beginner Guide
Variables are one of the most fundamental concepts in Rust programming. If you understand how variables work in Rust, you’ll build a strong foundation for mastering ownership, borrowing, memory safety, and performance.
Rust variables are powerful because they are:
Immutable by default
Strongly typed
Memory-safe
Ownership-aware
In this fully beginner guide, you’ll learn:
What variables are in Rust
How to declare variables
Mutability
Type annotations
Shadowing
Constants
Variable scope
Best practices
Common beginner mistakes
Let’s get started
What Are Variables in Rust?
A variable is a name that stores a value.
In Rust, variables are declared using the let keyword.
Basic example:
1 2 3 4 | fn main() { let x = 10; println!("{}", x); } |
Here:
letdeclares the variablexis the variable name10is the value
Rust automatically infers the type.
Immutable by Default (Very Important)
One of Rust’s unique features is:
Variables are immutable by default.
Example:
1 2 3 4 | fn main() { let x = 5; x = 10; // Error } |
Rust does this for safety and predictability.
Making Variables Mutable
If you want to change a variable, use mut.
1 2 3 4 5 | fn main() { let mut x = 5; x = 10; println!("{}", x); } |
Now x can be modified.
Why Rust Prefers Immutability
Immutability:
Prevents accidental changes
Reduces bugs
Makes code easier to understand
Helps with concurrency safety
Rust encourages safe programming habits.
Type Inference in Rust
Rust is statically typed, but it can infer types automatically.
1 | let number = 42; |
Rust understands this is an i32 by default.
Explicit Type Annotation
You can specify type manually:
1 2 3 | let age: u32 = 25; let pi: f64 = 3.14; let is_active: bool = true; |
Syntax:
1 | let variable_name: type = value; |
Common Rust Variable Types
| Type | Example |
|---|---|
| i32 | let x: i32 = 5; |
| f64 | let pi: f64 = 3.14; |
| bool | let is_ready: bool = true; |
| char | let letter: char = 'A'; |
| &str | let name = "Rust"; |
| String | let s = String::from("Rust"); |
Variables and Memory
Rust handles memory automatically.
When a variable goes out of scope, it is dropped.
Example:
1 2 3 4 | { let x = 10; } // x is dropped here |
Rust frees memory automatically.
Shadowing in Rust
Rust allows variable shadowing.
1 2 3 4 5 6 7 | fn main() { let x = 5; let x = x + 1; let x = x * 2; println!("{}", x); } |
Each let x creates a new variable.
Shadowing vs Mutability
Mutability changes value:
1 2 | let mut x = 5; x = 10; |
Shadowing creates new variable:
1 2 | let x = 5; let x = x + 5; |
Shadowing allows changing type.
Example:
1 2 | let spaces = " "; let spaces = spaces.len(); |
Type changes from &str to usize.
Constants in Rust
Constants are declared using const.
1 | const MAX_POINTS: u32 = 100; |
Rules:
Must declare type
Cannot use
mutValue must be constant expression
Naming convention: UPPERCASE
Difference Between let and const
| Feature | let | const |
|---|---|---|
| Mutable | Yes (with mut) | No |
| Type required | No | Yes |
| Evaluated at runtime | Yes | Compile-time |
| Scope limited | Yes | Global or local |
Destructuring Variables
Rust allows unpacking values.
Example with tuple:
1 | let (x, y) = (10, 20); |
Example with struct:
1 2 3 4 | struct Point { x: i32, y: i32 } let point = Point { x: 5, y: 10 }; let Point { x, y } = point; |
Variable Scope in Rust
Scope defines where variable is accessible.
1 2 3 4 5 6 7 8 9 10 | fn main() { let x = 10; { let y = 20; println!("{}", y); } // println!("{}", y); Error } |
Variables exist only inside their block.
Using Variables in Functions
1 2 3 | fn add(a: i32, b: i32) -> i32 { a + b } |
Parameters are also variables.
They follow ownership rules.
Variables and Ownership (Preview Concept)
For simple types:
1 2 | let x = 5; let y = x; // copied |
For String:
1 2 | let s1 = String::from("Hello"); let s2 = s1; // moved |
Understanding variables is crucial for ownership.
Visual Overview of Rust Variables
Best Practices for Rust Variables
- Prefer immutability
- Use meaningful names
- Use shadowing wisely
- Avoid unnecessary mut
- Keep scope small
Common Beginner Mistakes
- Forgetting
mut Confusing shadowing with mutability- Trying to change immutable variable
- Not understanding scope
- Ignoring type mismatches
Rust compiler gives helpful error messages.
Example – Real-World Variable Usage
1 2 3 4 5 6 7 8 | fn main() { const TAX_RATE: f64 = 0.18; let price = 100.0; let total_price = price + (price * TAX_RATE); println!("Total: {}", total_price); } |
Clean, readable, safe.
Frequently Asked Questions (FAQs)
1. Are variables mutable by default in Rust?
No. Variables are immutable by default.
2. How do I make a variable mutable?
Use the mut keyword.
3. What is shadowing in Rust?
Shadowing means declaring a new variable with the same name.
4. What is the difference between let and const?
let declares variables. const declares compile-time constants.
5. Does Rust require type annotations?
Not always. Rust can infer types automatically.
Conclusion
Rust variables are:
Immutable by default
Strongly typed
Memory-safe
Ownership-aware
You learned:
Variable declaration
Mutability
Type annotations
Shadowing
Constants
Scope
Understanding variables prepares you for advanced topics like ownership and borrowing.

