Rust for Loop

Rust Tutorial

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 for loop is

  • Basic 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:


 

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


 

Output:

1
2
3
4

Important:

  • 1..5 means start at 1

  • Stop before 5

This is called an exclusive range.


Inclusive Range

To include the last number:


 

Output:

1
2
3
4
5

The = makes it inclusive.


Visual Representation of Ranges

Rust for Loop

Looping Over Arrays


 

Rust automatically iterates safely.

No risk of out-of-bounds errors.


Looping Over Vectors


 

Important:

This moves ownership of the vector.

After loop, numbers cannot be used.


Borrowing While Looping

To avoid moving:


 

Now:

  • Vector is borrowed

  • Can still be used afterward


Mutable Iteration

To modify elements:


 

Explanation:

  • &mut creates mutable reference

  • *num dereferences value

Result:

[2, 3, 4]

Understanding Iterators

Behind the scenes, Rust uses iterators.

Equivalent to:


 

Is similar to:


 

Three main iterator types:

MethodDescription
into_iter()Takes ownership
iter()Immutable borrow
iter_mut()Mutable borrow

Example with Explicit Iterator


 


Loop Control Statements

break

Stops loop early.


 


continue

Skips current iteration.


 


Nested Loops


 

Useful for:

  • Grid layouts

  • Matrix operations

  • Pattern printing


Loop Labels (Advanced)

Rust supports labeled loops.


 

Breaks outer loop.


Looping Over Strings


 

Remember:

Rust strings are UTF-8.

To iterate bytes:


 


Loop with Index Using enumerate()


 

Very common pattern.


Using rev() to Reverse Loop


 

Output:

4
3
2
1

Comparing for Loop vs while Loop

Featureforwhile
Iterator-basedYesNo
SaferYesLess safe
Cleaner syntaxYesLonger
Index controlLimitedManual

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 for over while
  •  Use iter() for borrowing
  •  Use iter_mut() for modification
  •  Use enumerate() for index
  •  Use ranges carefully

Real-World Example – Sum of Numbers


 

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.

You may also like...