Iterator Adapters
Adapters transform iterators lazily: map, filter, take, skip, zip, flat_map, enumerate, peekable.
Search across all documentation pages
Adapters transform iterators lazily: map, filter, take, skip, zip, flat_map, enumerate, peekable.
fn main() {
let out: Vec<i32> = (0..10)
.filter(|n| n % 2 == 0)
.map(|n| n * 2)
.collect();
println!("{:?}", out);
}When to reach for this: Functional-style transforms without intermediate Vec unless needed.
fn parse_lines(lines: &str) -> Result<Vec<i32>, std::num::ParseIntError> {
lines.lines()
.map(|l| l.trim().parse())
.collect()
}
fn main() {
let data = "1\n2\n3\n";
println!("{:?}", parse_lines(data));
}What this demonstrates:
lines() adapter on &strmap + collect into Result<Vec<_>, _>collectChain order matters: filter before map avoids useless work. flat_map flattens Iterator<Item=impl Iterator>.
collect then reuse vec.move closure or owned data.zip_longest crates if needed.filter_map.| Alternative | Use When | Don't Use When |
|---|---|---|
for loop | Complex control flow | Simple transforms |
rayon par_iter | CPU parallel | I/O bound |
| Manual index | Neighbor access | Clean adapters exist |
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026