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