
Understand it
map and filter build a description of the work; nothing runs until a consumer such as collect, sum, or a for loop asks for items. That laziness is why a long chain still makes a single pass over the data instead of one pass per step.
An iterator is a sequence of values produced on demand. Calling .filter() or .map() builds another iterator; it does not run the closure over every element immediately. A consumer such as .collect(), .sum(), or a for loop pulls values through the chain. In the example, each requested output asks the filter for the next even input, then squares that value. There is no separate vector of filtered values unless you explicitly collect one.
This laziness can save allocations and allow early stopping. Add .take(2) before collecting and only two matching results are needed. Conversely, if you want to reuse results repeatedly, collecting may be the right choice. Be attentive to item types: .iter() yields references, .iter_mut() yields mutable references, and .into_iter() consumes the collection. A chain that looks visually similar can therefore borrow, mutate, or move its source.
For practice, insert a temporary inspect adapter that prints each value passing a stage. Observe that nothing prints before the consumer runs. Then change the consumer from collect to sum and predict the answer. Finally, try to use the original collection after .into_iter() and after .iter(). Iterator syntax is most readable when the operations tell a clear story—selection, transformation, then consumption—and when ownership of the source is intentional.
A lazy chain is useful only when a consumer eventually requests values. If you create an iterator and discard it, even a map closure with a print statement will never run. Use a loop for side effects when that intent is clearer.
Read the code
let squares: Vec<u32> = (1..=6)
.filter(|v| v % 2 == 0)
.map(|v| v * v)
.collect();Nothing runs until collect asks for items.
Practice starter
let list = ["ada", "grace"];
let names = list.iter().map(|n| n.to_uppercase());
println!("done");Try it
Sum only even integers with iterator adapters. Inputs and their sum fit i32. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
What work has been done by that point?
Reveal the answer
- Every name has already been uppercased
- None — nothing consumed the iterator
- Only the first element has been mapped
map returns a lazy adapter; without a consumer it never runs.
Go deeper
Common misconception: “.map() runs its closure over every element right away.”
map only builds a lazy adapter. No name is uppercased until a consumer such as collect, sum, or a for loop pulls items through names.
- Split the chain: In the example,
filterandmapare adapters over1..=6, andcollectis the consumer that fillssquares. - Predict before checking: Add
.inspect(|v| println!("saw {v}"))after the range, store the chain withoutcollect, printbuilt, and then collect. Predict which line prints first and whatsquaresholds. - Compare with evidence:
builtprints first,saw 1throughsaw 6follow duringcollect, andsquaresis[4, 16, 36].
Transfer challenge: Consume names with for name in names { println!("{name}"); } and check that the uppercase text now appears before done. Then make list a Vec<String>, switch to .into_iter(), and use list afterward to see the move.
Connections
- Closures: Fn, FnMut, FnOnce, and capture
- Writing an Iterator: impl Iterator and the next method
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: Iterators (courseId "ownership", lessonId "iterators") Link: https://crabrix.com/learn/ownership/iterators/ Goal: Adapters are lazy; a consumer is what makes the work happen. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/iterators) 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.