Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d7b8f46

Browse files
committedMar 15, 2025
Add experimental Iterator::contains
1 parent 4d30011 commit d7b8f46

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed
 

‎library/core/src/iter/traits/iterator.rs

+33
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,39 @@ pub trait Iterator {
27792779
self.try_fold((), check(f)) == ControlFlow::Break(())
27802780
}
27812781

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+
27822815
/// Searches for an element of an iterator that satisfies a predicate.
27832816
///
27842817
/// `find()` takes a closure that returns `true` or `false`. It applies

‎library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#![feature(internal_impls_macro)]
123123
#![feature(ip)]
124124
#![feature(is_ascii_octdigit)]
125+
#![feature(iter_contains)]
125126
#![feature(lazy_get)]
126127
#![feature(link_cfg)]
127128
#![feature(non_null_from_ref)]

‎library/core/src/str/iter.rs

+7
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ impl Iterator for Bytes<'_> {
331331
self.0.any(f)
332332
}
333333

334+
fn contains<Q: ?Sized>(&mut self, item: Q) -> bool
335+
where
336+
Q: PartialEq<Self::Item>,
337+
{
338+
self.0.contains(item)
339+
}
340+
334341
#[inline]
335342
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
336343
where

‎library/coretests/tests/iter/traits/iterator.rs

+10
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ fn test_any() {
271271
assert!(!v.iter().any(|&x| x > 100));
272272
assert!(!v[..0].iter().any(|_| panic!()));
273273
}
274+
#[test]
275+
fn test_iterator_contains() {
276+
let v: Box<[isize]> = Box::new([1, 2, 3, 4, 5]);
277+
assert_eq!(true, v.iter().contains(&3));
278+
assert_eq!(v.iter().contains(&3), v.iter().any(|&x| x == 3));
279+
assert_eq!(false, v.iter().contains(&10));
280+
assert_eq!(v.iter().contains(&10), v.iter().any(|&x| x == 10));
281+
assert_eq!(true, Iterator::contains(&mut (1isize..=5), 3));
282+
assert_eq!(false, Iterator::contains(&mut (1isize..=5), 10));
283+
}
274284

275285
#[test]
276286
fn test_find() {

‎library/coretests/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#![feature(iter_array_chunks)]
5050
#![feature(iter_chain)]
5151
#![feature(iter_collect_into)]
52+
#![feature(iter_contains)]
5253
#![feature(iter_intersperse)]
5354
#![feature(iter_is_partitioned)]
5455
#![feature(iter_map_windows)]

0 commit comments

Comments
 (0)
Failed to load comments.