about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2019-10-30 15:52:28 +0000
committerLzu Tao <taolzu@gmail.com>2019-10-30 15:52:28 +0000
commitbb1f4c47c1086e9290461676e9779fb0901f9da0 (patch)
tree477af8a66b689d1322a060b65d4af68d8a3832fe
parent0b7e28a1610924a27471ffdb59a2885709b3b415 (diff)
downloadrust-bb1f4c47c1086e9290461676e9779fb0901f9da0.tar.gz
rust-bb1f4c47c1086e9290461676e9779fb0901f9da0.zip
doc: reword iter module example and mention other methods
-rw-r--r--src/libcore/iter/mod.rs35
1 files changed, 14 insertions, 21 deletions
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs
index aba8e84d58b..fac6ff0f06b 100644
--- a/src/libcore/iter/mod.rs
+++ b/src/libcore/iter/mod.rs
@@ -118,26 +118,16 @@
 //!
 //! let mut counter = Counter::new();
 //!
-//! let x = counter.next().unwrap();
-//! println!("{}", x);
-//!
-//! let x = counter.next().unwrap();
-//! println!("{}", x);
-//!
-//! let x = counter.next().unwrap();
-//! println!("{}", x);
-//!
-//! let x = counter.next().unwrap();
-//! println!("{}", x);
-//!
-//! let x = counter.next().unwrap();
-//! println!("{}", x);
+//! assert_eq!(counter.next(), Some(1));
+//! assert_eq!(counter.next(), Some(2));
+//! assert_eq!(counter.next(), Some(3));
+//! assert_eq!(counter.next(), Some(4));
+//! assert_eq!(counter.next(), Some(5));
+//! assert_eq!(counter.next(), None);
 //! ```
 //!
-//! This will print `1` through `5`, each on their own line.
-//!
-//! Calling `next()` this way gets repetitive. Rust has a construct which can
-//! call `next()` on your iterator, until it reaches `None`. Let's go over that
+//! Calling [`next`] this way gets repetitive. Rust has a construct which can
+//! call [`next`] on your iterator, until it reaches `None`. Let's go over that
 //! next.
 //!
 //! Also note that `Iterator` provides a default implementation of methods such as `nth` and `fold`
@@ -253,20 +243,23 @@
 //! ```
 //!
 //! The idiomatic way to write a [`map`] for its side effects is to use a
-//! `for` loop instead:
+//! `for` loop or call the [`for_each`] method:
 //!
 //! ```
 //! let v = vec![1, 2, 3, 4, 5];
 //!
+//! v.iter().for_each(|x| println!("{}", x));
+//! // or
 //! for x in &v {
 //!     println!("{}", x);
 //! }
 //! ```
 //!
 //! [`map`]: trait.Iterator.html#method.map
+//! [`for_each`]: trait.Iterator.html#method.for_each
 //!
-//! The two most common ways to evaluate an iterator are to use a `for` loop
-//! like this, or using the [`collect`] method to produce a new collection.
+//! Another common way to evaluate an iterator is to use the [`collect`]
+//! method to produce a new collection.
 //!
 //! [`collect`]: trait.Iterator.html#method.collect
 //!