@@ -2779,6 +2779,39 @@ pub trait Iterator {
2779
2779
self . try_fold ( ( ) , check ( f) ) == ControlFlow :: Break ( ( ) )
2780
2780
}
2781
2781
2782
+ /// Tests whether a value is contained in the iterator.
2783
+ ///
2784
+ /// `contains()` is short-circuiting; in other words, it will stop processing
2785
+ /// as soon as the function finds the item in the `Iterator`.
2786
+ ///
2787
+ /// This method checks the whole iterator, which is O(n). If the iterator is a sorted
2788
+ /// slice, [`binary_search`](slice::binary_search) may be faster. If this is an iterator
2789
+ /// on collections that have a `.contains()` or `.contains_key()` method (such as
2790
+ /// `HashMap` or `BtreeSet`), using those methods directly will be faster.
2791
+ ///
2792
+ /// # Examples
2793
+ /// Basic usage:
2794
+ /// ```
2795
+ /// #![feature(iter_contains)]
2796
+ /// assert_eq!(true, [1, 2, 3].iter().contains(2));
2797
+ /// assert_eq!(false, [1, 2, 3].iter().contains(5));
2798
+ /// ```
2799
+ /// [`Iterator::contains`] can be used where [`slice::contains`] cannot be used:
2800
+ /// ```
2801
+ /// #![feature(iter_contains)]
2802
+ /// let s = [String::from("a"), String::from("b"), String::from("c")];
2803
+ /// assert_eq!(s.iter().contains("b"), s.iter().any(|e| e == "b"));
2804
+ /// ```
2805
+ #[ inline]
2806
+ #[ unstable( feature = "iter_contains" , reason = "new API" , issue = "127494" ) ]
2807
+ fn contains < Q : ?Sized > ( & mut self , item : Q ) -> bool
2808
+ where
2809
+ Q : PartialEq < Self :: Item > ,
2810
+ Self : Sized ,
2811
+ {
2812
+ self . any ( |elem| item == elem)
2813
+ }
2814
+
2782
2815
/// Searches for an element of an iterator that satisfies a predicate.
2783
2816
///
2784
2817
/// `find()` takes a closure that returns `true` or `false`. It applies
0 commit comments