How to zip With & lt; T & gt; with indexed position

advertisements

Following my question, How to iterate a Vec with indexed position in Rust, now I need to zip two dynamic vectors with their indexed position.


The enumerate function exists for all iterators. Using zip on two iterators a and b yields another iterator. Therefor you can also call enumerate on the resulting iterator.

fn main() {
    let a = vec![1; 10];
    let b = vec![2; 10];

    let it = a.iter().zip(b.iter());

    for (i, (x, y)) in it.enumerate() {
        println!("{}: ({}, {})", i, x, y);
    }
}