What Is the Difference Between Lazy and Eager Evaluation in Rust Iterators?

Rust iterators use lazy evaluation to defer computation until a consuming method is called, unlike eager evaluation which executes immediately.

Lazy evaluation means Rust iterators do nothing until you call a consuming method, while eager evaluation executes code immediately upon definition.

// Lazy: Does not print until collect() is called
let numbers = vec![1, 2, 3];
let doubled = numbers.iter().map(|x| {
    println!("Processing {}", x);
    x * 2
});

// Eager: Prints immediately when the loop starts
for n in numbers.iter().map(|x| {
    println!("Processing {}", x);
    x * 2
}) {
    println!("Got: {}", n);
}

// Force lazy evaluation to run
let result: Vec<_> = doubled.collect();

Rust iterators are lazy by default to allow chaining operations without intermediate allocations. You must call a consuming method like collect(), for_each(), or sum() to trigger the actual computation.