Zest.

Rust: Iterators: map, filter, collect

A short, tested Rust example for iterators: map, filter, collect. Copy it and adjust the names and paths for your project.

Rust
let nums = vec![1, 2, 3, 4];
let doubled: Vec<i32> = nums.iter().map(|n| n * 2).collect();
let evens: Vec<&i32> = nums.iter().filter(|n| *n % 2 == 0).collect();
let sum: i32 = nums.iter().sum();

Find it in Zest

Search for “rust iterators: map, filter, collect” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Rust examples

All Rust examples