@@ -59,6 +59,7 @@ use alloc::{
59
59
60
60
pub use either:: Either ;
61
61
62
+ use core:: borrow:: Borrow ;
62
63
#[ cfg( feature = "use_std" ) ]
63
64
use std:: collections:: HashMap ;
64
65
use std:: iter:: { IntoIterator , once} ;
@@ -1608,6 +1609,40 @@ pub trait Itertools : Iterator {
1608
1609
None
1609
1610
}
1610
1611
1612
+ /// Returns `true` if the given item is present in this iterator.
1613
+ ///
1614
+ /// This method is short-circuiting. If the given item is present in this
1615
+ /// iterator, this method will consume the iterator up-to-and-including
1616
+ /// the item. If the given item is not present in this iterator, the
1617
+ /// iterator will be exhausted.
1618
+ ///
1619
+ /// ```
1620
+ /// use itertools::Itertools;
1621
+ ///
1622
+ /// #[derive(PartialEq, Debug)]
1623
+ /// enum Enum { A, B, C, D, E, }
1624
+ ///
1625
+ /// let mut iter = vec![Enum::A, Enum::B, Enum::C, Enum::D].into_iter();
1626
+ ///
1627
+ /// // search `iter` for `B`
1628
+ /// assert_eq!(iter.contains(&Enum::B), true);
1629
+ /// // `B` was found, so the iterator now rests at the item after `B` (i.e, `C`).
1630
+ /// assert_eq!(iter.next(), Some(Enum::C));
1631
+ ///
1632
+ /// // search `iter` for `E`
1633
+ /// assert_eq!(iter.contains(&Enum::E), false);
1634
+ /// // `E` wasn't found, so `iter` is now exhausted
1635
+ /// assert_eq!(iter.next(), None);
1636
+ /// ```
1637
+ fn contains < Q > ( & mut self , query : & Q ) -> bool
1638
+ where
1639
+ Self : Sized ,
1640
+ Self :: Item : Borrow < Q > ,
1641
+ Q : PartialEq ,
1642
+ {
1643
+ self . any ( |x| x. borrow ( ) == query)
1644
+ }
1645
+
1611
1646
/// Check whether all elements compare equal.
1612
1647
///
1613
1648
/// Empty iterators are considered to have equal elements:
0 commit comments