Iterators & closures
14 steps in 5 sets of Rust.
Rust's iterators are lazy, zero-cost, and the idiomatic way to do almost anything to a collection. The adapters first, then closures and fold, then the terminal operations that actually make something happen.
The last set is maps and counting, which is where iterator chains stop being an exercise and start replacing loops you would otherwise have written by hand.
Adapters
- Iterator adaptersChaining map, filter, and collect over an iterator.
- enumerate and zipPairing iterator items with indexes or each other.
- Slicing a streamtake, skip and their while variants cut an iterator down without a loop.
- filter_map and flat_mapMapping and filtering in one pass, and flattening nested structure.
- zip, unzip and partitionPairing two streams, tearing pairs apart, splitting one stream in two.
Closures & folding
- ClosuresAnonymous functions that capture their environment.
- fold and reduceFolding an iterator into a single accumulated value.
- Laziness, peekable and scanAdapters do nothing until something pulls — which is what makes peeking and scanning cheap.
Terminal operations
- Asking questions of an iteratorThe terminal methods that answer a question rather than build a collection.
- Collecting into anythingcollect builds whatever type you ask for — map, string, or Result.
- Dedupe preserving orderRemoves duplicates while keeping the original order, which sorting would destroy.
Maps & counting
- HashMapThe key-value map and its entry API.
- Word count with HashMapTallies word frequencies in a HashMap, using the entry API.
Encore
- wordcount.rsA complete word-frequency program: read stdin, tally, sort, print.