@@ -146,6 +146,7 @@ pub mod structs {
146146 pub use crate :: repeatn:: RepeatN ;
147147 #[ allow( deprecated) ]
148148 pub use crate :: sources:: { RepeatCall , Unfold , Iterate } ;
149+ pub use crate :: take_while_inclusive:: TakeWhileInclusive ;
149150 #[ cfg( feature = "use_alloc" ) ]
150151 pub use crate :: tee:: Tee ;
151152 pub use crate :: tuple_impl:: { TupleBuffer , TupleWindows , CircularTupleWindows , Tuples } ;
@@ -233,6 +234,7 @@ mod rciter_impl;
233234mod repeatn;
234235mod size_hint;
235236mod sources;
237+ mod take_while_inclusive;
236238#[ cfg( feature = "use_alloc" ) ]
237239mod tee;
238240mod tuple_impl;
@@ -1457,6 +1459,74 @@ pub trait Itertools : Iterator {
14571459 adaptors:: take_while_ref ( self , accept)
14581460 }
14591461
1462+ /// Returns an iterator adaptor that consumes elements while the given
1463+ /// predicate is `true`, *including* the element for which the predicate
1464+ /// first returned `false`.
1465+ ///
1466+ /// The [`.take_while()`][std::iter::Iterator::take_while] adaptor is useful
1467+ /// when you want items satisfying a predicate, but to know when to stop
1468+ /// taking elements, we have to consume that first element that doesn't
1469+ /// satisfy the predicate. This adaptor includes that element where
1470+ /// [`.take_while()`][std::iter::Iterator::take_while] would drop it.
1471+ ///
1472+ /// The [`.take_while_ref()`][crate::Itertools::take_while_ref] adaptor
1473+ /// serves a similar purpose, but this adaptor doesn't require [`Clone`]ing
1474+ /// the underlying elements.
1475+ ///
1476+ /// ```rust
1477+ /// # use itertools::Itertools;
1478+ /// let items = vec![1, 2, 3, 4, 5];
1479+ /// let filtered: Vec<_> = items
1480+ /// .into_iter()
1481+ /// .take_while_inclusive(|&n| n % 3 != 0)
1482+ /// .collect();
1483+ ///
1484+ /// assert_eq!(filtered, vec![1, 2, 3]);
1485+ /// ```
1486+ ///
1487+ /// ```rust
1488+ /// # use itertools::Itertools;
1489+ /// let items = vec![1, 2, 3, 4, 5];
1490+ ///
1491+ /// let take_while_inclusive_result: Vec<_> = items
1492+ /// .iter()
1493+ /// .copied()
1494+ /// .take_while_inclusive(|&n| n % 3 != 0)
1495+ /// .collect();
1496+ /// let take_while_result: Vec<_> = items
1497+ /// .into_iter()
1498+ /// .take_while(|&n| n % 3 != 0)
1499+ /// .collect();
1500+ ///
1501+ /// assert_eq!(take_while_inclusive_result, vec![1, 2, 3]);
1502+ /// assert_eq!(take_while_result, vec![1, 2]);
1503+ /// // both iterators have the same items remaining at this point---the 3
1504+ /// // is lost from the `take_while` vec
1505+ /// ```
1506+ ///
1507+ /// ```rust
1508+ /// # use itertools::Itertools;
1509+ /// #[derive(Debug, PartialEq)]
1510+ /// struct NoCloneImpl(i32);
1511+ ///
1512+ /// let non_clonable_items: Vec<_> = vec![1, 2, 3, 4, 5]
1513+ /// .into_iter()
1514+ /// .map(NoCloneImpl)
1515+ /// .collect();
1516+ /// let filtered: Vec<_> = non_clonable_items
1517+ /// .into_iter()
1518+ /// .take_while_inclusive(|n| n.0 % 3 != 0)
1519+ /// .collect();
1520+ /// let expected: Vec<_> = vec![1, 2, 3].into_iter().map(NoCloneImpl).collect();
1521+ /// assert_eq!(filtered, expected);
1522+ fn take_while_inclusive < F > ( & mut self , accept : F ) -> TakeWhileInclusive < Self , F >
1523+ where
1524+ Self : Sized ,
1525+ F : FnMut ( & Self :: Item ) -> bool ,
1526+ {
1527+ take_while_inclusive:: TakeWhileInclusive :: new ( self , accept)
1528+ }
1529+
14601530 /// Return an iterator adaptor that filters `Option<A>` iterator elements
14611531 /// and produces `A`. Stops on the first `None` encountered.
14621532 ///
0 commit comments