mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:24:32 -06:00
Merge pull request #471 from DavidDeprost/patch-1
ch13-02-iterators.md fix
This commit is contained in:
@@ -234,7 +234,7 @@ For example, if for some reason we wanted to take the first five values that
|
|||||||
an instance of `Counter` produces, pair those values with values produced by
|
an instance of `Counter` produces, pair those values with values produced by
|
||||||
another `Counter` instance after skipping the first value that instance
|
another `Counter` instance after skipping the first value that instance
|
||||||
produces, multiply each pair together, keep only those results that are
|
produces, multiply each pair together, keep only those results that are
|
||||||
divisible by three, and add all the resulting values together, we could do:
|
divisible by three, and add all the resulting values together, we could do so:
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
let sum: u32 = Counter::new().take(5)
|
let sum: u32 = Counter::new().take(5)
|
||||||
@@ -242,9 +242,13 @@ let sum: u32 = Counter::new().take(5)
|
|||||||
.map(|(a, b)| a * b)
|
.map(|(a, b)| a * b)
|
||||||
.filter(|x| x % 3 == 0)
|
.filter(|x| x % 3 == 0)
|
||||||
.sum();
|
.sum();
|
||||||
assert_eq!(48, sum);
|
assert_eq!(18, sum);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note that zip() produces only four pairs; the theoretical fifth pair (5, None)
|
||||||
|
is never produced because zip() returns None when either of its input iterators
|
||||||
|
return None.
|
||||||
|
|
||||||
All of these method calls are possible because we implemented the `Iterator`
|
All of these method calls are possible because we implemented the `Iterator`
|
||||||
trait by specifying how the `next` method works. Use the standard library
|
trait by specifying how the `next` method works. Use the standard library
|
||||||
documentation to find more useful methods that will come in handy when you're
|
documentation to find more useful methods that will come in handy when you're
|
||||||
|
|||||||
Reference in New Issue
Block a user