
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.
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
- At .iter()
- When sum consumes it
- 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.
- Write the item types:
values.iter()yields&i32,filterhands its closure&&i32so the sample writes**v, andmapreceives&i32. - Predict before checking: Add
.inspect(|v| println!("kept {v}"))afterfilterand predict the printed lines andtotal. Then predict both again with.take(1)beforesum(). - Compare with evidence:
kept 2andkept 4print whilesumruns, andtotalis20. With.take(1), onlykept 2prints, andtotalis4.
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.