Chapter 16: Rust For Loop

Rust for loops

The for loop is Rust’s most idiomatic and most frequently used loop (in real projects written in 2025–2026, for appears in ~60–80% of all loop situations). Why? Because Rust turns almost everything into an iterator, and for works beautifully with them — it’s safe, expressive, clean, and prevents many classic off-by-one or bounds errors that plague other languages.

Let me teach this like we’re sitting together in a café near Hi-Tech City: slowly, with tons of copy-paste examples you can run right now (cargo run), everyday Hyderabad analogies (traffic counting, biryani ingredients, weekdays), comparisons to other languages, and building from simple to advanced patterns.

1. Basic Syntax – The Classic Form

Rust
  • VARIABLE — gets a new value each iteration (usually by value or reference)
  • ITERATOR — anything that implements IntoIterator (ranges, arrays, Vec, strings, maps, custom types…)
  • No parentheses needed around the condition
  • No manual increment/decrement needed — the iterator handles it

Simplest example — counting with a range:

Rust

Output:

text

Behind the scenes: 1..6 creates a Range<usize> struct. for calls .into_iter() on it → produces values one by one until empty.

2. Looping Over Arrays & Slices (Very Common)

Rust
  • weekdays → array implements IntoIterator → gives owned &str each time
  • .iter() → gives &str references (borrow, no move)
  • .enumerate() → pairs index + value (returns (usize, &T))

3. Looping Over Vec (Dynamic Lists)

Rust
  • &vec or &mut vec → borrow the collection
  • *level → dereference to mutate

4. For over Strings (Chars or Bytes)

Rust

5. for with break and continue (Same as other loops)

Rust

6. Nested for Loops + Labels (Useful for grids, matrices)

Rust

7. for as Expression? (No — but you can collect results)

for itself is not an expression (doesn’t return value), but you can use iterators + .collect():

Rust

This is the modern Rust way — prefer iterator chains over manual for + push when transforming data.

Quick Comparison Table (Teacher Style)

Loop Best For Manual Counter? Bounds Checking? Most Idiomatic in 2026 Rust?
for Known collection / range / iterator No Automatic ★★★★★ (yes!)
while Condition-based, no natural iterator Yes Manual ★★☆☆☆
loop Infinite / custom control flow Yes Manual ★★★☆☆

Summary in Teacher Voice

Rust for loop =

  • The go-to loop in modern Rust
  • Works on anything iterable (IntoIterator)
  • No manual indexing → fewer bugs
  • Clean syntax: for item in collection { … }
  • Loves ranges (1..=n), .iter(), .enumerate(), .into_iter()
  • Safe borrowing (&, &mut) built-in
  • Pairs beautifully with .map(), .filter(), .collect() for functional style

Practice challenge for today:

  1. Print weekdays with numbers using for + enumerate
  2. Make a vector of temperatures [28, 30, 32, 35, 38] → use for to print “hot” if >32
  3. Nested for to print a 5×5 multiplication table

Want solutions for these? Or next: iterators in depth (.map, .filter, .fold…)? Or functions + how for works inside them? Or ownership moves inside loops (very important topic)?

Just say the word — your Rust teacher is right here! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *