Skip to content

Chapter 5 of 8

Iterators, Closures, and Functional Patterns

Iterators are the foundation of Rust's approach to sequences. Any type that implements the `Iterator` trait must provide a `next()` method returning `Option`: `Some(item)` while elements remain and `None` once the stream is exhausted. Iterators are lazy — they do no work until something consumes them — which means they can describe complex pipelines without paying for results that are never used.

Iterator methods come in two flavors. Adapters such as `map()`, `filter()`, and `take()` return new iterators without doing any work themselves, so they can be chained freely. Consumers such as `collect()`, `sum()`, `count()`, and `for_each()` actually drive the iteration and produce a final value or side effect. A typical pipeline filters a range to keep only even numbers, squares each remaining element with `map`, and gathers the results into a `Vec` with `collect`, all written as a single readable expression.

Closures are anonymous functions that can capture variables from their enclosing scope. They appear constantly in iterator pipelines, where they serve as the arguments to `map`, `filter`, and similar adapters. The compiler classifies every closure's capture behavior with one of three traits: `Fn` for closures that capture by immutable reference, `FnMut` for those that capture by mutable reference, and `FnOnce` for those that consume their captured variables and can therefore be called only once. Every closure implements at least `FnOnce`, and many also implement `FnMut` or `Fn`.

The `move` keyword forces a closure to take ownership of everything it captures rather than borrowing. This is essential when a closure will outlive the scope in which it was created — for example, when it is sent to another thread — because the captured values must continue to live independently. Closures, iterators, and adapters together form a powerful functional style that compiles down to efficient imperative code.

All chapters
  1. 1Foundations of Rust Memory Safety
  2. 2Structures, Enums, and Pattern Matching
  3. 3Error Handling with Option and Result
  4. 4Traits, Generics, and Polymorphism
  5. 5Iterators, Closures, and Functional Patterns
  6. 6Modules, Crates, Cargo, and Testing
  7. 7Concurrency in Rust
  8. 8Smart Pointers and Unsafe Rust

Drill it

Reading is not remembering. These come from the Rust Programming deck:

Q

What is Rust?

Rust is a systems programming language focused on safety, speed, and concurrency. It achieves memory safety without a garbage collector through its ownership sy...

Q

What is ownership in Rust?

Ownership is Rust's core memory management concept. Each value has exactly one owner, and when the owner goes out of scope, the value is dropped (freed). This p...

Q

What are the three ownership rules in Rust?

Each value in Rust has a single ownerThere can only be one owner at a timeWhen the owner goes out of scope, the value is dropped

Q

What is borrowing in Rust?

Borrowing lets you reference a value without taking ownership. You create a reference with &. The original owner retains ownership while the borrower can re...