Abstractions in Practice · 16 min · free lesson

Iterator Pipelines

Transform lazily, then choose one consumer.

Core idea

Adapters such as map and filter describe work but do nothing until a consumer asks for values.

Coming from Node.js? Array methods vs iterators covers this side by side.

Diagram for Iterator Pipelines: a Rust code example flows to the lesson rule and the key observation.
The example makes the contract visible in code.

Understand it

Adapters such as map and filter describe work but do nothing until a consumer asks for values. A clear pipeline separates selection, transformation, and collection without temporary vectors.

An iterator pipeline can read like a data-processing sentence: choose items, transform each chosen item, then consume the result. The sample starts with borrowed values from an array, filters for even numbers, squares them, and sums the squares. The final answer is 20 for [1, 2, 3, 4]. The adapters are lazy; sum() repeatedly pulls the next item through the chain. No intermediate vector is required.

Look closely at references. values.iter() yields &i32, while filter passes a reference to each item into its predicate, so the closure sees an extra reference layer and the example uses **v. The following map receives each item as a plain &i32, and v * v needs no star because multiplication is also implemented for references. Writing types beside the closures for one exercise can demystify those stars. If you use into_iter() on an owned vector instead, values are moved into the pipeline, changing what remains available afterward.

Use inspect temporarily to see when each element reaches a stage. Then insert .take(1) before sum() and predict which elements are evaluated. Be careful where you place it: taking one input before a filter is different from taking one matched output after a filter. A pipeline becomes hard to read when it hides side effects in map or uses many conversions without names. Split it into intermediate bindings when the operations need explanation. The goal is a clear ownership and evaluation story, not the fewest lines.

Try replacing the array with a vector and selecting .iter() versus .into_iter(). With .into_iter() the filter closure sees &i32, so **v must become *v. The numeric answer stays the same, but the ownership of the source changes. That is part of the API contract.

Read the code

let values = [1, 2, 3, 4];
let total: i32 = values.iter()
    .filter(|v| **v % 2 == 0)
    .map(|v| v * v)
    .sum();

The example makes the contract visible in code.

Practise this code in Playground ↗

Try it

Mark each stage as adapter or consumer, and write the item type at each stage.

What success looks like: You can say why nothing runs until sum, and why the filter needs **v.

Check your understanding

When does the pipeline perform work?

Reveal the answer
  1. At .iter()
  2. When sum consumes it
  3. During parsing

Correct: When sum consumes it.

Go deeper

Common misconception: “The work happens at .iter(), one full pass per adapter, before sum runs.”

.iter(), filter, and map only build the chain. sum() pulls each value through it one at a time, so the closures first run when sum asks.

  1. Write the item types: values.iter() yields &i32, filter hands its closure &&i32 so the sample writes **v, and map receives &i32.
  2. Predict before checking: Add .inspect(|v| println!("kept {v}")) after filter and predict the printed lines and total. Then predict both again with .take(1) before sum().
  3. Compare with evidence: kept 2 and kept 4 print while sum runs, and total is 20. With .take(1), only kept 2 prints, and total is 4.

Transfer challenge: Replace the array with a Vec and switch to into_iter(). Change **v to *v, confirm that total is still 20, and then use the Vec again to see the move.

Connections

  • Generic Lifetimes: Combine type bounds with borrow relationships
  • Closure Capture Modes: Infer shared, mutable, or moved captures
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Iterator Pipelines (courseId "ownership", lessonId "iterator-adapters")
Link: https://crabrix.com/learn/ownership/iterator-adapters/
Goal: Adapters such as map and filter describe work but do nothing until a consumer asks for values.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/iterator-adapters) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.