Rust Tutorial: Iterators
The Iterator trait, lazy evaluation, map/filter/fold, and writing your own iterator.
Lazy sequences
Rust iterators are lazy — they do no work until consumed. This makes long chains efficient: only one pass over the data.
What you'll learn in this Rust iterators tutorial
This interactive Rust tutorial has 8 hands-on exercises. Estimated time: 20 minutes.
- iter() basics — `iter()` creates an iterator over references. `next()` advances it.
- map — `map` transforms each element. It is lazy — nothing happens until consumed.
- filter — `filter` keeps elements matching a predicate.
- fold — `fold` accumulates a value across all elements.
- enumerate — `enumerate` pairs each element with its index.
- zip — `zip` combines two iterators into pairs, stopping at the shorter.
- chain — `chain` concatenates two iterators.
- Custom iterator — Implement the `Iterator` trait for your own type by providing `next()`.
Rust Iterators concepts covered
- Lazy sequences