Iterator trait and combinators for zero-cost collection processing
Marcus Chen
Jan 2026
1 tab
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
let sum: i32 = numbers
.iter()
.filter(|&&x| x % 2 == 0)
.map(|&x| x * 2)
.sum();
println!("Sum of doubled evens: {}", sum);
}
1 file · rust
Explain with highlit
Rust's Iterator trait provides a rich set of combinators (.map(), .filter(), .fold(), etc.) that compose without allocating intermediate collections. Iterators are lazy: they don't do work until you consume them with .collect(), .for_each(), or similar. The compiler optimizes iterator chains into tight loops, often matching hand-written imperative code. I use iterators for data transformations, parsing, and anywhere I'd use loops in other languages. The key is that combinators are zero-cost abstractions: they're as fast as manual loops but more expressive and harder to get wrong. For parallel processing, rayon provides a drop-in replacement with .par_iter(). This functional style is idiomatic Rust and scales better than imperative loops.