Iterators & Closures

Closures are small anonymous functions that capture their environment; iterators are lazy sequences that transform collections. Chained together they replace most hand-written loops with expressive, zero-cost pipelines.

Closures

A closure is a function without a name that can capture variables from its surroundings. The || holds parameters. Rust assigns one of three traits depending on how the closure captures:

TraitCaptures byTypical use
Fnimmutable referenceread-only, call many times
FnMutmutable referencemay mutate captured state
FnOncevalue (moves)consumes capture, call once
fn main() {
    let prefix = String::from("Hello, ");

    // captures `prefix` by reference (Fn trait)
    let greet = |name: &str| format!("{prefix}{name}");
    println!("{}", greet("Rust"));

    let mut count = 0;
    // captures `count` mutably (FnMut trait)
    let mut inc = || { count += 1; count };
    println!("{} {} {}", inc(), inc(), inc());
}

Iterators

An iterator is a lazy sequence: nothing runs until you consume it. Call .iter() (borrow), .into_iter() (own), or .iter_mut() (mutable) to obtain one, then drive it with a for loop. Because iterators only produce values on demand, reversing or chaining them avoids intermediate heap allocations.

fn main() {
    let numbers = vec![1, 2, 3, 4, 5];

    for n in numbers.iter() {       // borrow each element
        print!("{n} ");
    }
    println!();

    for n in numbers.iter().rev() { // reverse without allocating
        print!("{n} ");
    }
    println!();
}

Combinators

Adapter methods transform one iterator into another lazily. Chain map, filter, and friends, then materialize the result only at the end with collect. The Rust compiler fuses these chained adapters into a single tight loop at compile time, matching hand-written C-style loops in performance without sacrificing readability.

fn main() {
    // square, keep even, sum the whole pipeline
    let total: i32 = (1..=10)
        .filter(|&n| n % 2 == 0)   // keep evens
        .map(|n| n * n)            // square them
        .sum();                    // consume (adds up)

    println!("sum of even squares = {total}");

    let doubled: Vec<i32> = (0..5).map(|n| n * 2).collect();
    println!("{doubled:?}");
}

Consuming Adapters

Every lazy pipeline needs a terminal consumer that drives next() until the iterator is exhausted or short-circuits. Methods like fold, count, any, all, and max_by_key reduce a stream to a single value, and short-circuiting consumers like any stop as soon as the outcome is known.

fn main() {
    let words = ["the", "quick", "brown", "fox"];

    let total_len: usize = words.iter().map(|w| w.len()).sum();
    let has_q = words.iter().any(|&w| w.contains('q'));
    let longest = words.iter().max_by_key(|w| w.len());

    println!("total len = {total_len}, has 'q' = {has_q}");
    println!("longest = {:?}", longest);
}

Watch for ownership when choosing between .iter() and .into_iter(): .into_iter() moves elements out of the collection so you cannot use the vector afterward, whereas .iter() borrows elements by reference and leaves the original collection intact.

Practice move: rewrite a for loop from the lab examples as a map/filter/collect chain and confirm the output is identical.