Iterator::any
Iterator::any is a function which when passed an iterator, will return
true if any element satisfies the predicate. Otherwise false. Its
signature:
1. pub trait Iterator {
2. // The type being iterated over.
3. type Item;
5. // `any` takes `&mut self` meaning the caller may be borrowed
6. // and modified, but not consumed.
7. fn any<F>(&mut self, f: F) -> bool where
8. // `FnMut` meaning any captured variable may at most be
9. // modified, not consumed. `Self::Item` states it takes
10. // arguments to the closure by value.
11. F: FnMut(Self::Item) -> bool {}
12. }
1. fn main() {
2. let vec1 = vec![1, 2, 3];
3. let vec2 = vec![4, 5, 6];
5. // `iter()` for vecs yields `&i32`. Destructure to `i32`.
6. println!("2 in vec1: {}", vec1.iter() .any(|&x| x == 2));
7. // `into_iter()` for vecs yields `i32`. No destructuring required.
8. println!("2 in vec2: {}", vec2.into_iter().any(| x| x == 2));
10. let array1 = [1, 2, 3];
11. let array2 = [4, 5, 6];
13. // `iter()` for arrays yields `&i32`.
14. println!("2 in array1: {}", array1.iter() .any(|&x| x == 2));
15. // `into_iter()` for arrays unusually yields `&i32`.
16. println!("2 in array2: {}", array2.into_iter().any(|&x| x == 2));
17. }
