Iterator Pipelines
Filter, Map, Collect
Transform Matching Items
Iterator pipelines can filter values, transform the survivors, and collect the final sequence.
Program
Play the program to choose the minimum value that passes through the pipeline.
filter_map_collect.rs
fn main() {
let min = ;
let values = [1, 3, 4, 6, 8];
let doubled: Vec<i32> = values.iter().copied().filter(|value| *value >= min).map(|value| value * 2).collect();
println!("{:?}", doubled);
}
fn main() {
let min = ;
let values = [1, 3, 4, 6, 8];
let doubled: Vec<i32> = values.iter().copied().filter(|value| *value >= min).map(|value| value * 2).collect();
println!("{:?}", doubled);
}
fn main() {
let min = ;
let values = [1, 3, 4, 6, 8];
let doubled: Vec<i32> = values.iter().copied().filter(|value| *value >= min).map(|value| value * 2).collect();
println!("{:?}", doubled);
}
filter
`filter` keeps only values that satisfy the predicate.
map
`map` transforms each surviving value into a new value.
collect
`collect` materializes the lazy iterator pipeline into a `Vec<i32>`.