Skip to content

Commit e72ee6e

Browse files
committed
Use a little more compelling example of for_each
1 parent 4a8ddac commit e72ee6e

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/libcore/iter/iterator.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,17 @@ pub trait Iterator {
500500
/// ```
501501
/// #![feature(iterator_for_each)]
502502
///
503-
/// let mut v = vec![];
504-
/// (0..5).for_each(|x| v.push(x * 100));
503+
/// use std::sync::mpsc::channel;
505504
///
506-
/// let mut v2 = vec![];
507-
/// for x in 0..5 { v2.push(x * 100); }
505+
/// let (tx, rx) = channel();
506+
/// (0..5).map(|x| x * 2 + 1)
507+
/// .for_each(move |x| tx.send(x).unwrap());
508508
///
509-
/// assert_eq!(v, v2);
509+
/// let v: Vec<_> = rx.iter().collect();
510+
/// assert_eq!(v, vec![1, 3, 5, 7, 9]);
510511
/// ```
511512
///
512-
/// For such a small example, the `for` loop is cleaner, but `for_each`
513+
/// For such a small example, a `for` loop may be cleaner, but `for_each`
513514
/// might be preferable to keep a functional style with longer iterators:
514515
///
515516
/// ```

0 commit comments

Comments
 (0)