@@ -24,24 +24,27 @@ fn main() {
2424 let vec1 = vec![1, 2, 3];
2525 let vec2 = vec![4, 5, 6];
2626
27- // `iter()` for vecs yields `&i32`.
27+ // `vec1. iter()` yields `&i32`.
2828 let mut iter = vec1.iter();
29- // `into_iter()` for vecs yields `i32`.
29+ // `vec2. into_iter()` yields `i32`.
3030 let mut into_iter = vec2.into_iter();
3131
32- // `iter()` for vecs yields `&i32`, and we want to reference one of its
33- // items, so we have to destructure `&&i32` to `i32`
34- println!("Find 2 in vec1: {:?}", iter .find(|&&x| x == 2));
35- // `into_iter()` for vecs yields `i32`, and we want to reference one of
36- // its items, so we have to destructure `&i32` to `i32`
37- println!("Find 2 in vec2: {:?}", into_iter.find(| &x| x == 2));
32+ // `iter()` yields `&i32`, and `find` passes `&Item` to the predicate.
33+ // Since `Item = &i32`, the closure argument has type `&&i32`,
34+ // which we pattern-match to dereference down to `i32`.
35+ println!("Find 2 in vec1: {:?}", iter.find(|&&x| x == 2));
36+
37+ // `into_iter()` yields `i32`, and `find` passes `&Item` to the predicate.
38+ // Since `Item = i32`, the closure argument has type `&i32`,
39+ // which we pattern-match to dereference down to `i32`.
40+ println!("Find 2 in vec2: {:?}", into_iter.find(|&x| x == 2));
3841
3942 let array1 = [1, 2, 3];
4043 let array2 = [4, 5, 6];
4144
42- // `iter()` for arrays yields `& &i32`
43- println!("Find 2 in array1: {:?}", array1.iter() .find(|&&x| x == 2));
44- // `into_iter()` for arrays yields `& i32`
45+ // `array1. iter()` yields `&i32`
46+ println!("Find 2 in array1: {:?}", array1.iter().find(|&&x| x == 2));
47+ // `array2. into_iter()` yields `i32`
4548 println!("Find 2 in array2: {:?}", array2.into_iter().find(|&x| x == 2));
4649}
4750```
@@ -53,13 +56,13 @@ item, use `Iterator::position`.
5356fn main() {
5457 let vec = vec![1, 9, 3, 3, 13, 2];
5558
56- // `iter()` for vecs yields `&i32` and `position()` does not take a reference, so
57- // we have to destructure `&i32` to `i32`
59+ // `position` passes the iterator’s `Item` by value to the predicate.
60+ // `vec.iter()` yields `&i32`, so the predicate receives `&i32`,
61+ // which we pattern-match to dereference to `i32`.
5862 let index_of_first_even_number = vec.iter().position(|&x| x % 2 == 0);
5963 assert_eq!(index_of_first_even_number, Some(5));
6064
61- // `into_iter()` for vecs yields `i32` and `position()` does not take a reference, so
62- // we do not have to destructure
65+ // `vec.into_iter()` yields `i32`, so the predicate receives `i32` directly.
6366 let index_of_first_negative_number = vec.into_iter().position(|x| x < 0);
6467 assert_eq!(index_of_first_negative_number, None);
6568}
0 commit comments