Iterator如何工作?

时间:2016-01-15 07:15:05

标签: rust

我不明白iter().all。这些代码来自文档:

let d = [2, 3, 4, 6, 8];
assert!(a.iter().all(|x| *x > 0));

为什么下面的那个不起作用?

for x in d.iter().all(|x| *x > 2) {
   println!("{} is bigger than 2", x);
}

1 个答案:

答案 0 :(得分:5)

all测试给定谓词对所有元素都适用。一旦找到与谓词不匹配的元素,它就会返回false,如果所有元素都匹配,则返回true。它不用于迭代匹配元素。您应该使用filter。请参阅the Iterator::all documentation