itertools

Macro izip

source
macro_rules! izip {
    ( @closure $p:pat => $tup:expr ) => { ... };
    ( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => { ... };
    ($first:expr $(,)*) => { ... };
    ($first:expr, $second:expr $(,)*) => { ... };
    ( $first:expr $( , $rest:expr )* $(,)* ) => { ... };
}
Expand description

Create an iterator running multiple iterators in lockstep.

The izip! iterator yields elements until any subiterator returns None.

This is a version of the standard .zip() that’s supporting more than two iterators. The iterator element type is a tuple with one element from each of the input iterators. Just like .zip(), the iteration stops when the shortest of the inputs reaches its end.

Note: The result of this macro is in the general case an iterator composed of repeated .zip() and a .map(); it has an anonymous type. The special cases of one and two arguments produce the equivalent of $a.into_iter() and $a.into_iter().zip($b) respectively.

Prefer this macro izip!() over multizip for the performance benefits of using the standard library .zip().


// iterate over three sequences side-by-side
let mut results = [0, 0, 0, 0];
let inputs = [3, 7, 9, 6];

for (r, index, input) in izip!(&mut results, 0..10, &inputs) {
    *r = index * 10 + input;
}

assert_eq!(results, [0 + 3, 10 + 7, 29, 36]);