Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental Iterator::contains #135018

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2770,6 +2770,39 @@ pub trait Iterator {
self.try_fold((), check(f)) == ControlFlow::Break(())
}

/// Tests whether a value is contained in the iterator.
///
/// `contains()` is short-circuiting; in other words, it will stop processing
/// as soon as the function finds the item in the `Iterator`.
///
/// This method checks the whole iterator, which is O(n). If the iterator is a sorted
/// slice, [`binary_search`](slice::binary_search) may be faster. If this is an iterator
/// on collections that have a `.contains()` or `.contains_key()` method (such as
/// `HashMap` or `BtreeSet`, using those methods directly will be faster.
Comment on lines +2780 to +2781
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// on collections that have a `.contains()` or `.contains_key()` method (such as
/// `HashMap` or `BtreeSet`, using those methods directly will be faster.
/// on collections that have a `.contains()` or `.contains_key()` method (such as
/// `HashMap` or `BtreeSet`), using those methods directly will be faster.

///
/// # Examples
/// Basic usage:
/// ```
/// #![feature(iter_contains)]
/// assert_eq!(true, [1, 2, 3].iter().contains(2));
/// assert_eq!(false, [1, 2, 3].iter().contains(5));
/// ```
/// [`Iterator::contains`] can be used where [`slice::contains`] cannot be used:
/// ```
/// #![feature(iter_contains)]
/// let s = [String::from("a"), String::from("b"), String::from("c")];
/// assert_eq!(s.iter().contains("b"), s.iter().any(|e| e == "b"));
/// ```
#[inline]
#[unstable(feature = "iter_contains", reason = "new API", issue = "127494")]
fn contains<Q: ?Sized>(&mut self, item: Q) -> bool
where
Q: PartialEq<Self::Item>,
Self: Sized,
{
self.any(|elem| item == elem)
}

/// Searches for an element of an iterator that satisfies a predicate.
///
/// `find()` takes a closure that returns `true` or `false`. It applies
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
#![feature(internal_impls_macro)]
#![feature(ip)]
#![feature(is_ascii_octdigit)]
#![feature(iter_contains)]
#![feature(lazy_get)]
#![feature(link_cfg)]
#![feature(non_null_from_ref)]
Expand Down
7 changes: 7 additions & 0 deletions library/core/src/str/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ impl Iterator for Bytes<'_> {
self.0.any(f)
}

fn contains<Q: ?Sized>(&mut self, item: Q) -> bool
where
Q: PartialEq<Self::Item>,
{
self.0.contains(item)
}

#[inline]
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
Expand Down
10 changes: 10 additions & 0 deletions library/core/tests/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ fn test_any() {
assert!(!v.iter().any(|&x| x > 100));
assert!(!v[..0].iter().any(|_| panic!()));
}
#[test]
fn test_iterator_contains() {
let v: Box<[isize]> = Box::new([1, 2, 3, 4, 5]);
assert_eq!(true, v.iter().contains(&3));
assert_eq!(v.iter().contains(&3), v.iter().any(|&x| x == 3));
assert_eq!(false, v.iter().contains(&10));
assert_eq!(v.iter().contains(&10), v.iter().any(|&x| x == 10));
assert_eq!(true, Iterator::contains(&mut (1isize..=5), 3));
assert_eq!(false, Iterator::contains(&mut (1isize..=5), 10));
}

#[test]
fn test_find() {
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#![feature(iter_array_chunks)]
#![feature(iter_chain)]
#![feature(iter_collect_into)]
#![feature(iter_contains)]
#![feature(iter_intersperse)]
#![feature(iter_is_partitioned)]
#![feature(iter_map_windows)]
Expand Down
Loading