]> git.lizzy.rs Git - rust.git/commitdiff
Use a little more compelling example of `for_each`
authorJosh Stone <jistone@redhat.com>
Tue, 27 Jun 2017 23:31:31 +0000 (16:31 -0700)
committerJosh Stone <jistone@redhat.com>
Tue, 27 Jun 2017 23:31:31 +0000 (16:31 -0700)
src/libcore/iter/iterator.rs

index d38864f3edddb4b7f28382d9cbdbc944c0350e8b..26660cb3331abd03da418db6c4877f0468c1187b 100644 (file)
@@ -500,16 +500,17 @@ fn map<B, F>(self, f: F) -> Map<Self, F> where
     /// ```
     /// #![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:
     ///
     /// ```