← All notes

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

Sources

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

Permanent ID: iil:note:x5ve940k

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.},
}

Markdown

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.

All artifact files (JSON index)

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.