Just use enumerate
:
Creates an iterator which gives the current iteration count as well as the next value.
fn main() {
let list: &[i32] = &vec![1, 3, 4, 17, 81];
for (i, el) in list.iter().enumerate() {
println!("The current element is {}", el);
println!("The current index is {}", i);
}
}
Output:
The current element is 1
The current index is 0
The current element is 3
The current index is 1
The current element is 4
The current index is 2
The current element is 17
The current index is 3
The current element is 81
The current index is 4