Collections & Iterators · 9 min read · Intermediate
Zero-Cost Iterators, Closures & Adapters
Chain lazy .iter(), .filter(), .map(), and .collect() pipelines that compile down to tight loops.
What you'll learn
- Transform collections lazily with map, filter, and fold
- Capture environment variables with closures (|x| ...)
- Collect iterator outputs into Vec, HashMap, or Result
Overview
Chain lazy .iter(), .filter(), .map(), and .collect() pipelines that compile down to tight loops.
Code Example
fn main() {
let nums = [1, 2, 3, 4, 5, 6];
let sum_even: i32 = nums.iter().copied().filter(|n| n % 2 == 0).sum();
println!("Even sum: {sum_even}");
}
Key Takeaways
- Transform collections lazily with map, filter, and fold
- Capture environment variables with closures (|x| …)
- Collect iterator outputs into Vec, HashMap, or Result
Quick Check
Check Your Understanding
Score: 0 / 1
1. When do Rust iterator adapters like `.map()` and `.filter()` actually execute?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.