@@ -2826,6 +2826,43 @@ pub trait Iterator {
2826
2826
self . try_fold ( ( ) , check ( f) ) == ControlFlow :: Break ( ( ) )
2827
2827
}
2828
2828
2829
+ /// Tests whether a value is contained in the iterator.
2830
+ ///
2831
+ /// `contains()` is short-circuiting; in other words, it will stop processing
2832
+ /// as soon as the function finds the item in the `Iterator`.
2833
+ ///
2834
+ /// This method checks the whole iterator, which is O(n). If the iterator is a sorted
2835
+ /// slice, [`binary_search`](slice::binary_search) may be faster. If this is an iterator
2836
+ /// on collections that have a `.contains()` or `.contains_key()` method (such as
2837
+ /// `HashMap` or `BtreeSet`), using those methods directly will be faster.
2838
+ ///
2839
+ /// # Examples
2840
+ ///
2841
+ /// Basic usage:
2842
+ ///
2843
+ /// ```
2844
+ /// #![feature(iter_contains)]
2845
+ /// assert!([1, 2, 3].iter().contains(2));
2846
+ /// assert!(![1, 2, 3].iter().contains(5));
2847
+ /// ```
2848
+ ///
2849
+ /// [`Iterator::contains`] can be used where [`slice::contains`] cannot be used:
2850
+ ///
2851
+ /// ```
2852
+ /// #![feature(iter_contains)]
2853
+ /// let s = [String::from("a"), String::from("b"), String::from("c")];
2854
+ /// assert!(s.iter().contains("b"));
2855
+ /// ```
2856
+ #[ inline]
2857
+ #[ unstable( feature = "iter_contains" , issue = "127494" ) ]
2858
+ fn contains < Q > ( & mut self , item : Q ) -> bool
2859
+ where
2860
+ Q : PartialEq < Self :: Item > + ?Sized ,
2861
+ Self : Sized ,
2862
+ {
2863
+ self . any ( |elem| item == elem)
2864
+ }
2865
+
2829
2866
/// Searches for an element of an iterator that satisfies a predicate.
2830
2867
///
2831
2868
/// `find()` takes a closure that returns `true` or `false`. It applies
0 commit comments