Rust for Loop

Rust for Loop – Complete Beginner Guide
Loops are essential in every programming language — and in Rust, the for loop is the most commonly used and safest way to iterate over data.
Whether you’re working with:
Arrays
Vectors
Ranges
Strings
Collections
The for loop in Rust is clean, safe, and powerful.
In this fully beginner guide, you’ll learn:
What the
forloop isBasic syntax
Looping over ranges
Looping over arrays and vectors
Iterators explained
Mutable vs immutable iteration
Loop control (
break,continue)Nested loops
Common mistakes
Best practices
Let’s dive in
What Is a for Loop in Rust?
A for loop in Rust is used to iterate over an iterator.
Basic syntax:
1 2 3 | for variable in iterable { // code } |
Unlike C-style loops (for (i = 0; i < n; i++)), Rust does not use traditional counter loops. Instead, it relies on iterators.
Basic Example – Looping Over a Range
1 2 3 4 5 | fn main() { for i in 1..5 { println!("{}", i); } } |
Output:
2
3
4
Important:
1..5means start at 1Stop before 5
This is called an exclusive range.
Inclusive Range
To include the last number:
1 2 3 | for i in 1..=5 { println!("{}", i); } |
Output:
2
3
4
5
The = makes it inclusive.
Visual Representation of Ranges
Looping Over Arrays
1 2 3 4 5 6 7 | fn main() { let numbers = [10, 20, 30, 40]; for num in numbers { println!("{}", num); } } |
Rust automatically iterates safely.
No risk of out-of-bounds errors.
Looping Over Vectors
1 2 3 4 5 6 7 | fn main() { let numbers = vec![1, 2, 3, 4]; for num in numbers { println!("{}", num); } } |
Important:
This moves ownership of the vector.
After loop, numbers cannot be used.
Borrowing While Looping
To avoid moving:
1 2 3 | for num in &numbers { println!("{}", num); } |
Now:
Vector is borrowed
Can still be used afterward
Mutable Iteration
To modify elements:
1 2 3 4 5 | let mut numbers = vec![1, 2, 3]; for num in &mut numbers { *num += 1; } |
Explanation:
&mutcreates mutable reference*numdereferences value
Result:
Understanding Iterators
Behind the scenes, Rust uses iterators.
Equivalent to:
1 2 | for item in collection { } |
Is similar to:
1 | collection.into_iter() |
Three main iterator types:
| Method | Description |
|---|---|
into_iter() | Takes ownership |
iter() | Immutable borrow |
iter_mut() | Mutable borrow |
Example with Explicit Iterator
1 2 3 4 5 | let v = vec![1, 2, 3]; for num in v.iter() { println!("{}", num); } |
Loop Control Statements
break
Stops loop early.
1 2 3 4 5 6 | for i in 1..10 { if i == 5 { break; } println!("{}", i); } |
continue
Skips current iteration.
1 2 3 4 5 6 | for i in 1..6 { if i == 3 { continue; } println!("{}", i); } |
Nested Loops
1 2 3 4 5 | for i in 1..4 { for j in 1..4 { println!("{} {}", i, j); } } |
Useful for:
Grid layouts
Matrix operations
Pattern printing
Loop Labels (Advanced)
Rust supports labeled loops.
1 2 3 4 5 6 7 | 'outer: for i in 1..5 { for j in 1..5 { if j == 3 { break 'outer; } } } |
Breaks outer loop.
Looping Over Strings
1 2 3 4 5 | let text = "Rust"; for ch in text.chars() { println!("{}", ch); } |
Remember:
Rust strings are UTF-8.
To iterate bytes:
1 2 3 | for b in text.bytes() { println!("{}", b); } |
Loop with Index Using enumerate()
1 2 3 4 5 | let numbers = vec![10, 20, 30]; for (index, value) in numbers.iter().enumerate() { println!("Index: {}, Value: {}", index, value); } |
Very common pattern.
Using rev() to Reverse Loop
1 2 3 | for i in (1..5).rev() { println!("{}", i); } |
Output:
3
2
1
Comparing for Loop vs while Loop
| Feature | for | while |
|---|---|---|
| Iterator-based | Yes | No |
| Safer | Yes | Less safe |
| Cleaner syntax | Yes | Longer |
| Index control | Limited | Manual |
Rust recommends for for iteration.
Common Beginner Mistakes
- Using moved vector after loop
- Forgetting
&for borrowing - Forgetting
*when modifying - Confusing exclusive vs inclusive range
- Trying C-style loops
Best Practices
- Prefer
foroverwhile Useiter()for borrowing- Use
iter_mut()for modification - Use
enumerate()for index - Use ranges carefully
Real-World Example – Sum of Numbers
1 2 3 4 5 6 | let numbers = vec![1, 2, 3, 4]; let mut sum = 0;for num in &numbers { sum += num; } println!("Sum: {}", sum); |
Clean and safe.
Frequently Asked Questions (FAQs)
1. What is a for loop in Rust?
A for loop iterates over an iterator like a range, array, or vector.
2. What is the difference between 1..5 and 1..=5?
1..5 excludes 5.1..=5 includes 5.
3. Does for loop move ownership?
Yes, unless you borrow using &.
4. How do I modify elements in a loop?
Use &mut and dereference with *.
5. Why doesn’t Rust use C-style for loops?
Rust uses iterators for safety and cleaner syntax.
Conclusion
Rust’s for loop is:
Safe
Iterator-based
Ownership-aware
Clean and expressive
You learned:
Ranges
Arrays and vectors
Borrowing
Mutable iteration
Loop control
Mastering for loops helps you write safe and efficient Rust programs.

