Skip to content

Commit 6371879

Browse files
committed
Update the guide examples and try not to leave user hanging as to what
this `&x` sigil is all about.
1 parent 0b5bc33 commit 6371879

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/doc/guide.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -4601,20 +4601,24 @@ returns `true` or `false`. The new iterator `filter()` produces
46014601
only the elements that that closure returns `true` for:
46024602

46034603
```{rust}
4604-
for i in range(1i, 100i).filter(|x| x % 2 == 0) {
4604+
for i in range(1i, 100i).filter(|&x| x % 2 == 0) {
46054605
println!("{}", i);
46064606
}
46074607
```
46084608

46094609
This will print all of the even numbers between one and a hundred.
4610+
(Note that because `filter` doesn't consume the elements that are
4611+
being iterated over, it is passed a reference to each element, and
4612+
thus the filter predicate uses the `&x` pattern to extract the integer
4613+
itself.)
46104614

46114615
You can chain all three things together: start with an iterator, adapt it
46124616
a few times, and then consume the result. Check it out:
46134617

46144618
```{rust}
46154619
range(1i, 1000i)
4616-
.filter(|x| x % 2 == 0)
4617-
.filter(|x| x % 3 == 0)
4620+
.filter(|&x| x % 2 == 0)
4621+
.filter(|&x| x % 3 == 0)
46184622
.take(5)
46194623
.collect::<Vec<int>>();
46204624
```

0 commit comments

Comments
 (0)