|
118 | 118 | //!
|
119 | 119 | //! let mut counter = Counter::new();
|
120 | 120 | //!
|
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); |
135 | 127 | //! ```
|
136 | 128 | //!
|
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 |
141 | 131 | //! next.
|
142 | 132 | //!
|
143 | 133 | //! Also note that `Iterator` provides a default implementation of methods such as `nth` and `fold`
|
|
253 | 243 | //! ```
|
254 | 244 | //!
|
255 | 245 | //! 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: |
257 | 247 | //!
|
258 | 248 | //! ```
|
259 | 249 | //! let v = vec![1, 2, 3, 4, 5];
|
260 | 250 | //!
|
| 251 | +//! v.iter().for_each(|x| println!("{}", x)); |
| 252 | +//! // or |
261 | 253 | //! for x in &v {
|
262 | 254 | //! println!("{}", x);
|
263 | 255 | //! }
|
264 | 256 | //! ```
|
265 | 257 | //!
|
266 | 258 | //! [`map`]: trait.Iterator.html#method.map
|
| 259 | +//! [`for_each`]: trait.Iterator.html#method.for_each |
267 | 260 | //!
|
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. |
270 | 263 | //!
|
271 | 264 | //! [`collect`]: trait.Iterator.html#method.collect
|
272 | 265 | //!
|
|
0 commit comments