Rust for loops: iterate a collection or a range
Answer: Rust has three kinds of loops: loop, while, and for. A for loop can execute some code for each item in a collection. The range a..b yields values from a inclusive to b exclusive.
for and range
The for in construct iterates through an Iterator.
Range notation a..b is one of the easiest ways to create an iterator.
Values run from a (inclusive) to b (exclusive), stepping by one.
By default the for loop will apply the into_iter function to the collection.
Why the book prefers for
Rust has three kinds of loops: loop, while, and for.
A more concise alternative is to execute some code for each item in a collection, using a for loop.
The example uses for element in a on an array of 10, 20, 30, 40, and 50.
This increases the safety of the code and reduces going beyond the end of the array.
Generated machine code can also skip comparing an index to the array length on each iteration.
Those qualities make for the most commonly used loop construct in Rust.
To repeat a set number of times, most Rustaceans still use a Range. The countdown writes for number in (1..4).rev().
See also
Claims
- id: C1 claim: Rust has three kinds of loops: loop, while, and for. source: S2 evidence: "Rust has three kinds of loops"
- id: C2 claim: A for loop can execute some code for each item in a collection. source: S2 evidence: "execute some code for each item in a collection"
- id: C3 claim: Range a..b yields values from a inclusive to b exclusive. source: S1 evidence: "from a (inclusive) to b (exclusive)"
- id: C4 claim: By default, for applies into_iter to the collection. source: S1 evidence: "apply the into_iter function to the collection"
- id: C5 claim: for loops are the most commonly used loop construct in Rust. source: S2 evidence: "the most commonly used loop construct in Rust"
Sources
- [S1] for loops. Rust by Example. https://doc.rust-lang.org/rust-by-example/flow_control/for.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] Control Flow. The Rust Programming Language. https://doc.rust-lang.org/book/ch03-05-control-flow.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Rust by Example, for loops; The Rust Programming Language, Control Flow. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Rust by Example, for loops; The Rust Programming Language, Control Flow. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Rust for loops: iterate a collection or a range." iinventedlight Library, iil:note:x5ve940k, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-for-loops/. License: CC-BY-4.0.
@misc{iil_note_x5ve940k,
author = {{iinventedlight}},
title = {{Rust for loops: iterate a collection or a range}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-for-loops/},
note = {Permanent ID iil:note:x5ve940k. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-for-loops.schema.json (schema; CC-BY-4.0)
- rust-for-loops.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-for-loops.md.