-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Adding the has_item function to the iterator trait. #127604
Changes from all commits
c068fdc
c143b42
7931998
fd40ecb
5372e4b
82d481d
15d6c9f
48616ce
7ba6c5e
b69db01
3751b5d
7ebf719
ac8d1e3
cd839b8
66c55b2
b67ee44
1ba1ca7
934a194
3e3ccd8
bc20afd
8a7c12b
dac6cd7
2a32a8c
ba5b980
e70effd
82b6b10
2f72670
fb98975
d8fc920
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1327,7 +1327,7 @@ pub trait Iterator { | |||||||||||||||
/// `take(n)` yields elements until `n` elements are yielded or the end of | ||||||||||||||||
/// the iterator is reached (whichever happens first). | ||||||||||||||||
/// The returned iterator is a prefix of length `n` if the original iterator | ||||||||||||||||
/// contains at least `n` elements, otherwise it contains all of the | ||||||||||||||||
/// contains at least `n` elements, otherwise it containss all of the | ||||||||||||||||
/// (fewer than `n`) elements of the original iterator. | ||||||||||||||||
/// | ||||||||||||||||
/// # Examples | ||||||||||||||||
|
@@ -4060,6 +4060,40 @@ pub trait Iterator { | |||||||||||||||
{ | ||||||||||||||||
unreachable!("Always specialized"); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Returns `true` if the iterator contains a value. | ||||||||||||||||
/// | ||||||||||||||||
/// 'contains' is short-circuiting; in other words, it will stop processing | ||||||||||||||||
/// as soon as the function finds the item in the `Iterator`. | ||||||||||||||||
/// | ||||||||||||||||
/// Performance: | ||||||||||||||||
/// This method checks the whole iterator, which takes O(n) time. | ||||||||||||||||
/// If the iterator is sorted, or a hash map please use the appropriate method instead. | ||||||||||||||||
Comment on lines
+4069
to
+4071
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We can be more specific about what the appropriate methods are here. No section heading needed, I think rustdoc would have just merged You can check the rendering with |
||||||||||||||||
/// | ||||||||||||||||
/// Example: | ||||||||||||||||
/// ``` | ||||||||||||||||
/// #![feature(iter_contains)] | ||||||||||||||||
/// assert!([1, 2, 3].iter().map(|&x| x * 3).contains(&6)); | ||||||||||||||||
/// assert!(![1, 2, 3].iter().contains(&4)); | ||||||||||||||||
/// // You can check if a String is in a string slice iterator. | ||||||||||||||||
/// assert!(["a", "b", "c"].iter().contains(&"b".to_owned())); | ||||||||||||||||
/// // You can also check if a String iterator contains a string slice. | ||||||||||||||||
/// assert!(["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contains(&"b")); | ||||||||||||||||
/// ``` | ||||||||||||||||
/// | ||||||||||||||||
#[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, | ||||||||||||||||
{ | ||||||||||||||||
for element in self { | ||||||||||||||||
if item == element { | ||||||||||||||||
return true; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
return false; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Compares two iterators element-wise using the given function. | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -616,6 +616,65 @@ fn test_next_chunk() { | |
let mut it = std::iter::repeat_with(|| panic!()); | ||
assert_eq!(it.next_chunk::<0>().unwrap(), []); | ||
} | ||
#[test] | ||
fn test_happy_path_item_not_in_iterator() { | ||
assert!(![1i32, 2i32, 3i32].iter().contains(&4i32)); | ||
} | ||
|
||
#[test] | ||
fn test_edge_case_handling_none_values() { | ||
assert!([Some(2i32), Option::<i32>::None].iter().contains(&None)); | ||
assert!([Some(2i32), Option::<i32>::None].iter().contains(&Some(2i32))); | ||
} | ||
|
||
#[test] | ||
fn test_edge_case_handling_empty_iterator() { | ||
assert!(!Vec::<i32>::new().iter().contains(&1i32)); | ||
} | ||
|
||
#[test] | ||
fn test_edge_case_handling_iterator_with_duplicates() { | ||
assert!([1i32, 2i32, 2i32, 3i32].iter().contains(&2i32)); | ||
} | ||
|
||
#[test] | ||
/// Tests that short-circuiting works correctly when using `contains` | ||
/// When you run the function, it should move the iterator forward after the first appearance of the item | ||
fn test_short_circuiting() { | ||
let vector: Vec<i32> = vec![1i32, 2i32, 3i32, 1i32, 1i32]; | ||
let mut iterator = vector.into_iter(); | ||
assert!(iterator.contains(1i32)); | ||
assert_eq!(iterator.next(), Some(2)); | ||
assert!(!iterator.contains(4i32)); | ||
assert_eq!(iterator.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_edge_case_handling_iterator_with_custom_struct() { | ||
Comment on lines
+619
to
+653
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: since these tests only check |
||
#[derive(PartialEq)] | ||
struct Item { | ||
value: i32, | ||
} | ||
assert!([Item { value: 1i32 }, Item { value: 2i32 }].iter().contains(&Item { value: 2i32 })); | ||
} | ||
|
||
#[test] | ||
fn test_str_iterator_contains_string() { | ||
assert!(["a", "b", "c"].iter().contains(&"b".to_owned())); | ||
assert!(!["a", "b", "c"].iter().contains(&"d".to_owned())); | ||
} | ||
|
||
#[test] | ||
fn test_str_iterator_contains_string_slice() { | ||
assert!(["a", "b", "c"].iter().contains(&"b")); | ||
assert!(!["a", "b", "c"].iter().contains(&"d")); | ||
} | ||
|
||
#[test] | ||
fn test_string_iterator_contains_str_slice() { | ||
assert!(["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contains(&"b")); | ||
assert!(!["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contains(&"d")); | ||
} | ||
|
||
// just tests by whether or not this compiles | ||
fn _empty_impl_all_auto_traits<T>() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6074,6 +6074,17 @@ The tracking issue for this feature is: None. | |
|
||
------------------------ | ||
|
||
"##, | ||
}, | ||
Lint { | ||
label: "contains", | ||
description: r##"# `contains` | ||
|
||
The tracking issue for this feature is: [#94047] | ||
|
||
[#94047]: https://github.com/rust-lang/rust/issues/94047 | ||
|
||
|
||
Comment on lines
+6077
to
+6087
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is autogenerated so no need for adding this (its also adding it one slot too late) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yonikremer see this comment too, you can revert this file |
||
The `rustc` compiler has certain pluggable operations, that is, | ||
functionality that isn't hard-coded into the language, but is | ||
implemented in libraries, with a special marker to tell the compiler | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
fn main() { | ||
let vec = Vec::new(); | ||
Vec::contains(&vec, &0); | ||
//~^ ERROR no function or associated item named `contains` found for struct `Vec<_, _>` in the current scope | ||
//~^ ERROR `Vec<_, _>` is not an iterator [E0599] | ||
//~| HELP the function `contains` is implemented on `[_]` | ||
} | ||
Comment on lines
1
to
6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bit of an unfortunate suggestion change for an unstable method. This was added in #100302, cc @compiler-errors since this will no longer test what it was intended to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls file a bug, we probably can filter by stability or sth idk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.