Iterators are the foundation of Rust's approach to sequences. Any type that implements the `Iterator` trait must provide a `next()` method returning `Option
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
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.