Skip to content

Commit 5540605

Browse files
committed
Rollup merge of rust-lang#31351 - steveklabnik:gh31318, r=alexcrichton
This is a behavior that some find confusing, so it deserves its own example. Fixes rust-lang#31318 I think this wording might be a bit strange, but I couldn't come up with anything better. Feedback very welcome.
2 parents 688f607 + 69c298e commit 5540605

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libcore/iter.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,30 @@ pub trait Iterator {
10501050
/// // got a false, take_while() isn't used any more
10511051
/// assert_eq!(iter.next(), None);
10521052
/// ```
1053+
///
1054+
/// Because `take_while()` needs to look at the value in order to see if it
1055+
/// should be included or not, consuming iterators will see that it is
1056+
/// removed:
1057+
///
1058+
/// ```
1059+
/// let a = [1, 2, 3, 4];
1060+
/// let mut iter = a.into_iter();
1061+
///
1062+
/// let result: Vec<i32> = iter.by_ref()
1063+
/// .take_while(|n| **n != 3)
1064+
/// .cloned()
1065+
/// .collect();
1066+
///
1067+
/// assert_eq!(result, &[1, 2]);
1068+
///
1069+
/// let result: Vec<i32> = iter.cloned().collect();
1070+
///
1071+
/// assert_eq!(result, &[4]);
1072+
/// ```
1073+
///
1074+
/// The `3` is no longer there, because it was consumed in order to see if
1075+
/// the iteration should stop, but wasn't placed back into the iterator or
1076+
/// some similar thing.
10531077
#[inline]
10541078
#[stable(feature = "rust1", since = "1.0.0")]
10551079
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where

0 commit comments

Comments
 (0)