Chapter 23: Iterators, the Rust Superpower
Prerequisites
You will understand
- Lazy evaluation — nothing runs until a consumer pulls
- Zero-cost: iterator chains compile to the same code as hand-written loops
- Key adapters:
filter,map,collect,fold
Reading time
35 min
+ 25 min exercises
You'll need this for Chapter 25Every iterator chain relies on the
Iterator trait and its next() method. Chapter 25 shows how traits like Iterator are defined, implemented, and composed.Preview Ch 25 →Lazy Evaluation
Iterator Pipelines Do Nothing Until a Consumer Pulls
Zero Cost
Iterator Chain vs Hand-Written Loop
Readiness Check - Iterator Pipeline Reasoning
| Skill | Level 0 | Level 1 | Level 2 | Level 3 |
|---|---|---|---|---|
| Understand laziness | I assume each adapter runs immediately | I know consumers trigger execution | I can explain when and why work is deferred | I can predict runtime behavior of complex chains confidently |
| Track ownership through iteration | I confuse iter/iter_mut/into_iter | I can name borrow vs move differences | I can select iteration mode intentionally per use case | I can refactor loops and chains without ownership regressions |
| Diagnose type flow in chains | I patch until compile passes | I can read one adapter signature at a time | I can locate exact type mismatch stage in a long chain | I can design reusable iterator-based APIs with clean bounds |
Target Level 2+ before trait-heavy iterator implementation work.
Compiler Error Decoder - Iterator Chains
| Error code | What it usually means | Typical fix direction |
|---|---|---|
| E0282 | Type inference is ambiguous (often around collect) | Add target type annotation (Vec<_>, HashMap<_, _>, etc.) at collection boundary |
| E0599 | Adapter/consumer not available on current type | Confirm you are on an iterator (call iter()/into_iter() when needed) |
| E0382 | Value moved unexpectedly by ownership-taking iteration | Borrow with iter() or clone intentionally if ownership must be retained |
Debug chain failures by splitting the pipeline into named intermediate variables and checking each type.