Skip to content

Commit bb1f4c4

Browse files
committed
doc: reword iter module example and mention other methods
1 parent 0b7e28a commit bb1f4c4

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/libcore/iter/mod.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,16 @@
118118
//!
119119
//! let mut counter = Counter::new();
120120
//!
121-
//! let x = counter.next().unwrap();
122-
//! println!("{}", x);
123-
//!
124-
//! let x = counter.next().unwrap();
125-
//! println!("{}", x);
126-
//!
127-
//! let x = counter.next().unwrap();
128-
//! println!("{}", x);
129-
//!
130-
//! let x = counter.next().unwrap();
131-
//! println!("{}", x);
132-
//!
133-
//! let x = counter.next().unwrap();
134-
//! println!("{}", x);
121+
//! assert_eq!(counter.next(), Some(1));
122+
//! assert_eq!(counter.next(), Some(2));
123+
//! assert_eq!(counter.next(), Some(3));
124+
//! assert_eq!(counter.next(), Some(4));
125+
//! assert_eq!(counter.next(), Some(5));
126+
//! assert_eq!(counter.next(), None);
135127
//! ```
136128
//!
137-
//! This will print `1` through `5`, each on their own line.
138-
//!
139-
//! Calling `next()` this way gets repetitive. Rust has a construct which can
140-
//! call `next()` on your iterator, until it reaches `None`. Let's go over that
129+
//! Calling [`next`] this way gets repetitive. Rust has a construct which can
130+
//! call [`next`] on your iterator, until it reaches `None`. Let's go over that
141131
//! next.
142132
//!
143133
//! Also note that `Iterator` provides a default implementation of methods such as `nth` and `fold`
@@ -253,20 +243,23 @@
253243
//! ```
254244
//!
255245
//! The idiomatic way to write a [`map`] for its side effects is to use a
256-
//! `for` loop instead:
246+
//! `for` loop or call the [`for_each`] method:
257247
//!
258248
//! ```
259249
//! let v = vec![1, 2, 3, 4, 5];
260250
//!
251+
//! v.iter().for_each(|x| println!("{}", x));
252+
//! // or
261253
//! for x in &v {
262254
//! println!("{}", x);
263255
//! }
264256
//! ```
265257
//!
266258
//! [`map`]: trait.Iterator.html#method.map
259+
//! [`for_each`]: trait.Iterator.html#method.for_each
267260
//!
268-
//! The two most common ways to evaluate an iterator are to use a `for` loop
269-
//! like this, or using the [`collect`] method to produce a new collection.
261+
//! Another common way to evaluate an iterator is to use the [`collect`]
262+
//! method to produce a new collection.
270263
//!
271264
//! [`collect`]: trait.Iterator.html#method.collect
272265
//!

0 commit comments

Comments
 (0)