有没有更直接、更易读的方法来完成以下任务:
fn main() {
let a = [1, 2, 3];
let b = [4, 5, 6];
let c = [7, 8, 9];
let iter = a.iter()
.zip(b.iter())
.zip(c.iter())
.map(|((x, y), z)| (x, y, z));
}
也就是说,如何从 n 个可迭代对象构建一个生成 n 元组的迭代器?
您可以使用izip!()
板条箱中的宏迭代工具 https://crates.io/crates/itertools,它为任意多个迭代器实现了这一点:
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) {
}
}
您必须在 Cargo.toml 中添加对 itertools 的依赖项,使用最新版本。例子:
[dependencies]
itertools = "0.8"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)