Home 12. Enumerate
Post
Cancel

12. Enumerate

Use enumerate to convert iterator over (usize,item) pairs.

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

for (idx, car) in cars.iter().enumerate() {
    println!("{idx} - {car}");
}

// Output
// 0 - Skoda
// 1 - Ferrari
// 2 - Ford

Useful when index of item is needed.

This post is licensed under CC BY 4.0 by the author.