Function itertools::multizip

source ·
pub fn multizip<T, U>(t: U) -> Zip<T>
where Zip<T>: From<U> + Iterator,
Expand description

An iterator that generalizes .zip() and allows running multiple iterators in lockstep.

The iterator Zip<(I, J, ..., M)> is formed from a tuple of iterators (or values that implement IntoIterator) and yields elements until any of the subiterators yields None.

The iterator element type is a tuple like like (A, B, ..., E) where A to E are the element types of the subiterator.

Note: The result of this function is a value of a named type (Zip<(I, J, ..)> of each component iterator I, J, ...) if each component iterator is nameable.

Prefer izip!() over multizip for the performance benefits of using the standard library .zip(). Prefer multizip if a nameable type is needed.

use itertools::multizip;

// 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 multizip((&mut results, 0..10, &inputs)) {
    *r = index * 10 + input;
}

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