File tree 1 file changed +24
-0
lines changed
1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -1050,6 +1050,30 @@ pub trait Iterator {
1050
1050
/// // got a false, take_while() isn't used any more
1051
1051
/// assert_eq!(iter.next(), None);
1052
1052
/// ```
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.
1053
1077
#[ inline]
1054
1078
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1055
1079
fn take_while < P > ( self , predicate : P ) -> TakeWhile < Self , P > where
You can’t perform that action at this time.
0 commit comments