How can I zip more than two iterators?

You could also create a macro using the .zip provided like,

$ cat z.rs
macro_rules! zip {
    ($x: expr) => ($x);
    ($x: expr, $($y: expr), +) => (
        $x.iter().zip(
            zip!($($y), +))
    )
}


fn main() {
    let x = vec![1,2,3];
    let y = vec![4,5,6];
    let z = vec![7,8,9];

    let zipped = zip!(x, y, z);
    println!("{:?}", zipped);
    for (a, (b, c)) in zipped {
        println!("{} {} {}", a, b, c);
    }
}

Output:

$ rustc z.rs && ./z
Zip { a: Iter([1, 2, 3]), b: Zip { a: Iter([4, 5, 6, 67]), b: IntoIter([7, 8, 9]), index: 0, len: 0 }, index: 0, len: 0 }
1 4 7
2 5 8
3 6 9

You can use the izip!() macro from the crate itertools, which implements this for arbitrary many iterators:

use itertools::izip;

fn main() {

    let a = [1, 2, 3];
    let b = [4, 5, 6];
    let c = [7, 8, 9];

    // izip!() accepts iterators and/or values with IntoIterator.
    for (x, y, z) in izip!(&a, &b, &c) {

    }
}

You would have to add a dependency on itertools in Cargo.toml, use whatever version is the latest. Example:

[dependencies]
itertools = "0.8"

I wanted to be able to do this to an arbitrarily long vector, so I had to implement this by hand:

fn transpose_records<T: Clone>(records: &Vec<Vec<T>>) -> Vec<Vec<T>> {
    let mut transposed: Vec<Vec<T>> = vec![Vec::new(); records[0].len()];

    for record in records {
        for (index, element) in record.iter().enumerate() {
            transposed[index].push(element.clone());
        }
    }

    transposed
}

Tags:

Iterator

Rust