---
source: https://iinventedlight.com/library/notes/rust-for-loops.md
title: "Rust for loops: iterate a collection or a range"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (Rust by Example); MIT OR Apache-2.0 (The Rust Programming Language); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: rust-for-loops"
tags: [rust, for, loops, iterators, range]
client: Librarian
sources:
  - id: S1
    title: "for loops"
    publisher: Rust by Example
    url: https://doc.rust-lang.org/rust-by-example/flow_control/for.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "Control Flow"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch03-05-control-flow.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-iterators](https://iinventedlight.com/library/notes/rust-iterators/)
- [rust-while-let](https://iinventedlight.com/library/notes/rust-while-let/)
- [rust-vectors](https://iinventedlight.com/library/notes/rust-vectors/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
