diff options
| author | Josh Stone <jistone@redhat.com> | 2017-06-27 16:31:31 -0700 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2017-06-27 16:31:31 -0700 |
| commit | e72ee6e4ad0511aaf533a492382b84dfa712393f (patch) | |
| tree | 01ca8f48c44277ca45e9ccd0e6ff6a5cfec20a39 | |
| parent | 4a8ddac99e1edfb219e11c3ea2d6c43ccecb29ab (diff) | |
| download | rust-e72ee6e4ad0511aaf533a492382b84dfa712393f.tar.gz rust-e72ee6e4ad0511aaf533a492382b84dfa712393f.zip | |
Use a little more compelling example of `for_each`
| -rw-r--r-- | src/libcore/iter/iterator.rs | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index d38864f3edd..26660cb3331 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -500,16 +500,17 @@ pub trait Iterator { /// ``` /// #![feature(iterator_for_each)] /// - /// let mut v = vec![]; - /// (0..5).for_each(|x| v.push(x * 100)); + /// use std::sync::mpsc::channel; /// - /// let mut v2 = vec![]; - /// for x in 0..5 { v2.push(x * 100); } + /// let (tx, rx) = channel(); + /// (0..5).map(|x| x * 2 + 1) + /// .for_each(move |x| tx.send(x).unwrap()); /// - /// assert_eq!(v, v2); + /// let v: Vec<_> = rx.iter().collect(); + /// assert_eq!(v, vec![1, 3, 5, 7, 9]); /// ``` /// - /// For such a small example, the `for` loop is cleaner, but `for_each` + /// For such a small example, a `for` loop may be cleaner, but `for_each` /// might be preferable to keep a functional style with longer iterators: /// /// ``` |
