File tree 1 file changed +7
-3
lines changed
1 file changed +7
-3
lines changed Original file line number Diff line number Diff line change @@ -4601,20 +4601,24 @@ returns `true` or `false`. The new iterator `filter()` produces
4601
4601
only the elements that that closure returns ` true ` for:
4602
4602
4603
4603
``` {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) {
4605
4605
println!("{}", i);
4606
4606
}
4607
4607
```
4608
4608
4609
4609
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.)
4610
4614
4611
4615
You can chain all three things together: start with an iterator, adapt it
4612
4616
a few times, and then consume the result. Check it out:
4613
4617
4614
4618
``` {rust}
4615
4619
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)
4618
4622
.take(5)
4619
4623
.collect::<Vec<int>>();
4620
4624
```
You can’t perform that action at this time.
0 commit comments