Skip to content
Tutorials Center
My learning

Topics

Lesson 52 of 132 ยท Collections and Iterators

Iterator Patterns and Collecting

Master Rust iterator recipes - collect into Vec, HashMap, String and Result, flat_map, scan, take_while, partition, unzip, chunk_by, fold-based grouping and hand-written itertools helpers.

The previous lesson taught the protocol: next, adapters and consumers. This lesson is a cookbook. Real programs rarely stop at map and sum; they turn lists of text into maps, flatten nested data, stop early, keep running totals, split a list in two, and bail out on the first error. Rust's standard library has a small, sharp tool for each of those jobs, and collect() is the most flexible of them.

We will go through the patterns in the order you are likely to need them. Every example runs in the browser, and at the end we write a few helpers by hand that people often install an "itertools" crate for.

collect() builds whatever you ask for

collect() consumes an iterator and builds a collection. Which collection? That is decided by the type you ask for, not by collect itself. It works for any type that implements FromIterator, so the standard collections, String and even Result all qualify.

Rust One iterator, many collections

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Notice the two ways to name the target type: a type annotation on the variable (let unique: BTreeSet<&str> = ...) or the turbofish on the method (collect::<HashSet<_>>()). The _ lets the compiler infer the element type. If you give neither, rustc says error[E0283]: type annotations needed, because it cannot guess what to build.

When two items share a key, a map collected from pairs keeps the last one. That is a handy way to deduplicate by key, and a quiet source of bugs if you did not expect it.

collect into Result and Option

This is one of Rust's best tricks. If every item is a Result<T, E>, you can collect into Result<Vec<T>, E>. You get Ok(vec) if all items succeeded, or the first Err (the iterator stops right there).

Rust Collecting Results and Options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Pick the style that matches your intent: collecting into Result means "all or nothing", while filter_map(... .ok()) means "best effort, skip the junk". The Result lesson and the ? operator build on this.

flat_map and flatten

map produces one output per input. flat_map lets each input produce zero or more outputs, and joins them into a single stream. flatten is the special case where items are already iterable (nested vectors, Options, Results).

Rust flat_map and flatten

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The move in the last closure copies n into the inner closure; without it the inner closure would borrow a variable that disappears at the end of the outer closure. Closures explains why.

Stopping and tracking: take_while, skip_while, scan

take(n) stops after n items. take_while(pred) stops at the first item where the predicate is false, and skip_while skips items until the predicate first turns false. scan is like fold but yields every intermediate state, so you get running totals.

Rust take_while, skip_while and scan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Careful: take_while consumes the first failing item to find out that it fails. If you then keep using the same iterator, that item is gone. With peekable() and next_if you can stop without losing it.

Splitting and joining: partition, unzip, zip

partition splits one stream into two collections by a predicate. unzip turns an iterator of pairs into a pair of collections. zip goes the other way.

Rust partition, unzip and zip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Grouping with fold and chunk_by

Rust's standard Iterator has no group_by; the "word count" pattern is a fold into a map using the entry API:

Rust Group and count with fold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

chunk_by is a slice method (stable since Rust 1.77). It only groups adjacent items: in [1, 1, 2, 3, 3, 3, 1] the final 1 starts a new run instead of joining the first one. For global grouping, use a map.

Recipes by hand

Crates like itertools add helpers such as join, dedup, sorted and tuple_windows. You cannot install crates in this browser runner (they need Cargo), but most of the useful ones are a few lines:

Rust Writing small helpers yourself

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The signature impl Iterator<Item = (T, T)> + '_ means "some iterator that borrows from the argument". The next lesson covers returning iterators in depth.

Pitfalls and design notes

  • Do not collect needlessly. let v: Vec<_> = it.collect(); v.len() allocates for nothing; call count(). Collect only when you need the data twice or need random access.
  • Do not collect in the middle of a chain just to continue the chain. Keep it lazy until the end.
  • Annotate sums and collects. sum() and collect() are generic over the result type. Write let t: i32 = ... or ::<i32>.
  • Order matters for filter and map. Filter first when the filter is cheap and map is expensive, so less work is done.
  • HashMap order is random. Collecting into a HashMap and printing it gives a different order from run to run. Sort or use BTreeMap for stable output.
  • Failing silently with filter_map(.ok()) hides bad input. Use it only when "skip bad rows" is truly the requirement.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What decides which collection collect() builds?

    collect() is generic over any FromIterator type. You choose the target with let x: HashSet<_> = ... or collect::<HashSet<_>>(); otherwise rustc reports that type annotations are needed.
  2. 2. What does ["1", "x", "3"].iter().map(|s| s.parse::<i32>()).collect::<Result<Vec<_>, _>>() return?

    Collecting into Result<Vec<T>, E> returns the first Err it meets and stops. Use filter_map(|s| s.parse().ok()) if you want to skip bad items instead.
  3. 3. What does flat_map do compared with map?

    flat_map(f) is map(f).flatten(): the closure returns something iterable and all those items are concatenated.
  4. 4. Given [1, 1, 2, 1], what does chunk_by(|a, b| a == b) produce?

    chunk_by only groups adjacent items. The final 1 is separated from the first two by the 2, so it starts its own run.
  5. 5. What is the difference between scan and fold?

    fold is a consumer returning one value. scan is a lazy adapter that yields a value per item, which is ideal for running totals, and it can stop early by returning None.

Summary

  • collect() builds the type you request: Vec, sets, maps, String, VecDeque, and also Result<Vec<T>, E> or Option<Vec<T>> (all or nothing).
  • flat_map and flatten join nested or optional items into one stream.
  • take_while, skip_while and scan control where iteration starts and stops and keep running state.
  • partition, unzip and zip split and join streams of pairs.
  • Group with fold plus the entry API, or chunk_by for adjacent runs.
  • Small helpers (join, min_max, pairs) replace most "itertools" needs.
  • Avoid pointless intermediate collects, and annotate result types.

Next: Writing Your Own Iterators