std-method

Iterator::zip

Pair items from two sequences. For ordinary finite sequences the number of pairs is at most the length of the shorter source. Do not use it as a length-equality check. Expected stdout (not executed): [("tea", 4), ("rice", 9)] Common mistake: Assuming zip reports mismatched lengths.

Formal Syntax & Structure

rust syntaxrust
fn zip<U: IntoIterator>(self, other: U) -> Zip<Self, U::IntoIter> where Self: Sized

Working Example

Example Snippetrust
fn main() {    let order: Vec<_> = ["tea", "rice", "salt"].into_iter().zip([4, 9]).collect();    println!("{order:?}");}
Rust Reference