Skip to main content
Module 8 · Lesson 2 of 316/24 in the course~20 min
Module lessons (2/3)

Trait Iterator and Lazy Adapters (map, filter, zip)

Lesson preview5 hands-on exercises readyCourse Rust · Module 8 — Closures and Iterators Zero-Cost

Why iterators don't work until consumed, difference between .iter(), .into_iter(), .iter_mut() and chained adapters.

What you'll learn in this lesson

  • Choose precisely between .iter() (&T), .iter_mut() (&mut T) and .into_iter() (T by value).
  • Compose declarative pipelines with lazy adapters (.map(), .filter(), .enumerate(), .zip(), .take()).
  • Understand why adapter chains do nothing until they encounter a consumer method.

Overview

The standard trait Iterator requires only one fundamental method: fn next(&mut self) -> Option<Self::Item>. Dozens of composable methods with zero intermediate allocations are supported on this single brick.

Methods like .map(), .filter(), .filter_map(), .take(n) and .zip(other) don't iterate right away: they return a new wrapping iterator (wrapper) struct, which will execute one element at a time when consumed:

Sections covered in this lesson

  1. 11. Three different ways to create an iterator
  2. 22. Lazy Adapters: Construct transformations without executing
  3. 33. .copied() and .cloned() on reference iterators
  4. 44. filter_map to clean and convert in one go
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 8 — Closures and Iterators Zero-Cost

Unlock "Trait Iterator and Lazy Adapters (map, filter, zip)" and the full Course Rust curriculum

Why iterators don't work until consumed, difference between `.iter()`, `.into_iter()`, `.iter_mut()` and chained adapters.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Rust plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.