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
forfor arrays, slices, vectors, ranges, and other iterable values. - ✅ Use
whilewhen 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
forloops when later code still needs ownership.
Pitfalls
- ⚠️ Manual index loops can panic if the length or condition changes; see Arrays.
- ⚠️
whileconditions are not truthy; they must evaluate toboolunless usingwhile let. - ⚠️ A
forloop may move the iterated value; use references if that is not intended. - ⚠️
break valueis forloop, not ordinarywhileorforloops.
See also
Loop Expressions · Arrays · Slices · Vectors · Iterators · If Expressions · Iterator Method Trio · Ownership · Basic Concepts & Syntax
Sources
- The Rust Programming Language, ch. 3.5 “Conditional Loops with
while” — the-book, https://doc.rust-lang.org/book/ch03-05-control-flow.html#conditional-loops-with-while - The Rust Programming Language, ch. 3.5 “Looping Through a Collection with
for” — the-book, https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for - The Rust Reference, “Predicate loops” — the-reference, https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-loops
- The Rust Reference, “Iterator loops” — the-reference, https://doc.rust-lang.org/reference/expressions/loop-expr.html#iterator-loops
