Skip to content

Commit ae96e51

Browse files
committed
Rollup merge of rust-lang#31220 - steveklabnik:gh30632, r=nikomatsakis
Fixes rust-lang#30632 I'm not sure if this explanation is good enough. If it is, I will add it to filter as well.
2 parents 76e0025 + 7deb057 commit ae96e51

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/libcore/iter.rs

+43
Original file line numberDiff line numberDiff line change
@@ -3258,6 +3258,49 @@ impl<A, B> DoubleEndedIterator for Zip<A, B> where
32583258
///
32593259
/// [`map()`]: trait.Iterator.html#method.map
32603260
/// [`Iterator`]: trait.Iterator.html
3261+
///
3262+
/// # Notes about side effects
3263+
///
3264+
/// The [`map()`] iterator implements [`DoubleEndedIterator`], meaning that
3265+
/// you can also [`map()`] backwards:
3266+
///
3267+
/// ```rust
3268+
/// let v: Vec<i32> = vec![1, 2, 3].into_iter().rev().map(|x| x + 1).collect();
3269+
///
3270+
/// assert_eq!(v, [4, 3, 2]);
3271+
/// ```
3272+
///
3273+
/// [`DoubleEndedIterator`]: trait.DoubleEndedIterator.html
3274+
///
3275+
/// But if your closure has state, iterating backwards may act in a way you do
3276+
/// not expect. Let's go through an example. First, in the forward direction:
3277+
///
3278+
/// ```rust
3279+
/// let mut c = 0;
3280+
///
3281+
/// for pair in vec!['a', 'b', 'c'].into_iter()
3282+
/// .map(|letter| { c += 1; (letter, c) }) {
3283+
/// println!("{:?}", pair);
3284+
/// }
3285+
/// ```
3286+
///
3287+
/// This will print "('a', 1), ('b', 2), ('c', 3)".
3288+
///
3289+
/// Now consider this twist where we add a call to `rev`. This version will
3290+
/// print `('c', 1), ('b', 2), ('a', 3)`. Note that the letters are reversed,
3291+
/// but the values of the counter still go in order. This is because `map()` is
3292+
/// still being called lazilly on each item, but we are popping items off the
3293+
/// back of the vector now, instead of shifting them from the front.
3294+
///
3295+
/// ```rust
3296+
/// let mut c = 0;
3297+
///
3298+
/// for pair in vec!['a', 'b', 'c'].into_iter()
3299+
/// .map(|letter| { c += 1; (letter, c) })
3300+
/// .rev() {
3301+
/// println!("{:?}", pair);
3302+
/// }
3303+
/// ```
32613304
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
32623305
#[stable(feature = "rust1", since = "1.0.0")]
32633306
#[derive(Clone)]

0 commit comments

Comments
 (0)