Home 9. Inspecting iterator
Post
Cancel

9. Inspecting iterator

Ever wondered how to print while iterating?

Use inspect.

1
2
3
4
5
6
7
8
9
10
let cars = vec!["Skoda", "Ferrari", "Ford"];

let car_lenghts: Vec<u32> = cars
    .iter()
    .enumerate()
    .inspect(|(idx, s)| println!("{idx} - {s}"))
    .map(|(_, name)| name.len() as u32)
    .collect();

assert!( car_lenghts == vec![5,7,4
This post is licensed under CC BY 4.0 by the author.