How to zip iterators

Use Iterator::zip for two iterators or itertools::izip for multiple to combine items into tuples.

Use the itertools crate's izip function to zip multiple iterators into a single iterator of tuples. Add itertools to your Cargo.toml dependencies and import the trait to access the method.

use itertools::Itertools;

let a = vec![1, 2, 3];
let b = vec!['a', 'b', 'c'];
let c = vec![10.0, 20.0, 30.0];

let zipped = a.iter().zip(b.iter()).zip(c.iter());
// Or with itertools for more than 2 iterators:
// let zipped = a.iter().izip(b.iter()).izip(c.iter());

for (x, y, z) in zipped {
    println!("{}, {}, {}", x, y, z);
}

For exactly two iterators, use the standard library's Iterator::zip method directly without extra dependencies:

let a = vec![1, 2, 3];
let b = vec!['a', 'b', 'c'];

for (x, y) in a.iter().zip(b.iter()) {
    println!("{}, {}", x, y);
}