While and For Loops

while repeats while a boolean or conditional pattern succeeds, while for iterates over values from IntoIterator and is the usual choice for collections.

What it is

while condition { ... } runs a block repeatedly as long as the condition remains true. It is best for state-driven loops where the condition is not simply “each item in this collection.”

for pattern in expression { ... } loops over values produced by an iterator. It is Rust’s idiomatic collection loop and avoids manual index bookkeeping.

Both forms evaluate to (); use Loop Expressions with break value when the loop itself must compute a non-unit result.

How it works

A while condition must be bool or a conditional let match. After each successful body execution, the condition is evaluated again.

A for expression calls IntoIterator::into_iter on the loop expression, repeatedly calls Iterator::next, and binds each yielded value to the loop pattern. The Reference specifies this desugaring in terms of standard IntoIterator, Iterator, and Option.

For arrays, for element in array consumes or copies elements depending on the element type and context. Use for element in &array when you want references instead of moving values.

Example

fn main() {
    let mut countdown = 3;
    while countdown > 0 {
        countdown -= 1;
    }
    assert_eq!(countdown, 0);
 
    let values = [10, 20, 30];
    let mut sum = 0;
    for value in values {
        sum += value;
    }
    assert_eq!(sum, 60);
}

Common errors

Iterating by value can move a collection:

fn main() {
    let names = vec![String::from("Ferris")];
    for name in names {
        println!("{name}");
    }
    // println!("{}", names.len());
}

Typical diagnostic:

error[E0382]: borrow of moved value: `names`

Fix with for name in &names when later code still needs the collection.

Best practice

  • ✅ Use for for arrays, slices, vectors, ranges, and other iterable values.
  • ✅ Use while when the loop condition is external state or a repeated predicate.
  • ✅ Prefer ranges such as (1..4).rev() over manual counter loops for fixed numeric repetition.
  • ✅ Borrow collections in for loops when later code still needs ownership.

Pitfalls

  • ⚠️ Manual index loops can panic if the length or condition changes; see Arrays.
  • ⚠️ while conditions are not truthy; they must evaluate to bool unless using while let.
  • ⚠️ A for loop may move the iterated value; use references if that is not intended.
  • ⚠️ break value is for loop, not ordinary while or for loops.

See also

Loop Expressions · Arrays · Slices · Vectors · Iterators · If Expressions · Iterator Method Trio · Ownership · Basic Concepts & Syntax

Sources