How to use map and filter

Use filter to select items and map to transform them in a Rust iterator chain before collecting the result.

Use map to transform every item in an iterator and filter to keep only items matching a condition, chaining them before calling collect to produce a new collection.

let numbers = vec![1, 2, 3, 4, 5];
let result: Vec<u32> = numbers
    .iter()
    .filter(|&&n| n % 2 == 0)
    .map(|&n| n * 2)
    .collect();
// result is [4, 8, 12]