Function std::iter::zip

1.59.0 · source ·
pub fn zip<A, B>(
    a: A,
    b: B
) -> Zip<<A as IntoIterator>::IntoIter, <B as IntoIterator>::IntoIter> where
    A: IntoIterator,
    B: IntoIterator,
Expand description

将参数转换为迭代器并压缩它们。

有关更多信息,请参见 Iterator::zip 的文档。

Examples

use std::iter::zip;

let xs = [1, 2, 3];
let ys = [4, 5, 6];

let mut iter = zip(xs, ys);

assert_eq!(iter.next().unwrap(), (1, 4));
assert_eq!(iter.next().unwrap(), (2, 5));
assert_eq!(iter.next().unwrap(), (3, 6));
assert!(iter.next().is_none());

// 嵌套 zip 也可以:
let zs = [7, 8, 9];

let mut iter = zip(zip(xs, ys), zs);

assert_eq!(iter.next().unwrap(), ((1, 4), 7));
assert_eq!(iter.next().unwrap(), ((2, 5), 8));
assert_eq!(iter.next().unwrap(), ((3, 6), 9));
assert!(iter.next().is_none());
Run