Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update iter_find.md #1512

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/fn/closures/closure_examples/iter_find.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ item, use `Iterator::position`.
fn main() {
let vec = vec![1, 9, 3, 3, 13, 2];

let index_of_first_even_number = vec.iter().position(|x| x % 2 == 0);
// `iter()` for vecs yields `&i32` and `position()` does not take a reference, so
// we have to destructure `&i32` to `i32`
let index_of_first_even_number = vec.iter().position(|&x| x % 2 == 0);
assert_eq!(index_of_first_even_number, Some(5));


let index_of_first_negative_number = vec.iter().position(|x| x < &0);
// `into_iter()` for vecs yields `i32` and `position()` does not take a reference, so
// we do not have to destructure
let index_of_first_negative_number = vec.into_iter().position(|x| x < 0);
assert_eq!(index_of_first_negative_number, None);
}
```
Expand Down