Skip to content

Commit 7ebf719

Browse files
committed
getting arguments that implement arg == Self::Item and not argument where Self::Item implement Self::Item == arg
1 parent 3751b5d commit 7ebf719

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,20 +4072,33 @@ pub trait Iterator {
40724072
/// Example:
40734073
/// ```
40744074
/// #![feature(contains)]
4075-
/// assert!([1, 2, 3].iter().contain(&1));
4076-
/// assert!(![1, 2, 3].iter().contain(&4));
4077-
/// assert!([Some(2), None].iter().contain(&None));
4078-
/// assert!([Some(2), None].iter().contain(&Some(2)));
4075+
/// assert!(![1i32, 2i32, 3i32].iter().contain(&4i32));
4076+
/// assert!([Some(2i32), Option::<i32>::None].iter().contain(&None));
4077+
/// assert!([Some(2i32), Option::<i32>::None].iter().contain(&Some(2i32)));
4078+
/// assert!(!Vec::<i32>::new().iter().contain(&1i32));
4079+
/// assert!([1i32, 2i32, 2i32, 3i32].iter().contain(&2i32));
4080+
/// #[derive(PartialEq)]
4081+
/// struct Item {
4082+
/// value: i32,
4083+
/// }
4084+
/// assert!([Item { value: 1i32 }, Item { value: 2i32 }].iter().contain(&Item { value: 2i32 }));
4085+
/// assert!(["a", "b", "c"].iter().contain(&"b".to_owned()));
4086+
/// assert!(!["a", "b", "c"].iter().contain(&"d".to_owned()));
4087+
/// assert!(["a", "b", "c"].iter().contain(&"b"));
4088+
/// assert!(!["a", "b", "c"].iter().contain(&"d"));
4089+
/// assert!(["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contain(&"b"));
4090+
/// assert!(!["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contain(&"d"));
4091+
/// assert!((1..1000).contain(500i32));
40794092
/// ```
40804093
///
40814094
#[unstable(feature = "contains", reason = "new API", issue = "127494")]
4082-
fn contain<Q: Sized>(&mut self, item: Q) -> bool
4095+
fn contain<Q: ?Sized>(&mut self, item: Q) -> bool
40834096
where
4084-
Self::Item: PartialEq<Q>,
4097+
Q: PartialEq<Self::Item>,
40854098
Self: Sized,
40864099
{
40874100
for element in self {
4088-
if element == item {
4101+
if item == element {
40894102
return true;
40904103
}
40914104
}

library/core/tests/iter/traits/iterator.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -618,23 +618,23 @@ fn test_next_chunk() {
618618
}
619619
#[test]
620620
fn test_happy_path_item_not_in_iterator() {
621-
assert!(![1, 2, 3].iter().contain(&4));
621+
assert!(![1i32, 2i32, 3i32].iter().contain(&4i32));
622622
}
623623

624624
#[test]
625625
fn test_edge_case_handling_none_values() {
626-
assert!([Some(2), None].iter().contain(&None));
627-
assert!([Some(2), None].iter().contain(&Some(2)));
626+
assert!([Some(2i32), Option::<i32>::None].iter().contain(&None));
627+
assert!([Some(2i32), Option::<i32>::None].iter().contain(&Some(2i32)));
628628
}
629629

630630
#[test]
631631
fn test_edge_case_handling_empty_iterator() {
632-
assert!(!Vec::<i32>::new().iter().contain(&1));
632+
assert!(!Vec::<i32>::new().iter().contain(&1i32));
633633
}
634634

635635
#[test]
636636
fn test_edge_case_handling_iterator_with_duplicates() {
637-
assert!([1, 2, 2, 3].iter().contain(&2));
637+
assert!([1i32, 2i32, 2i32, 3i32].iter().contain(&2i32));
638638
}
639639

640640
#[test]
@@ -643,14 +643,14 @@ fn test_edge_case_handling_iterator_with_custom_struct() {
643643
struct Item {
644644
value: i32,
645645
}
646-
assert!([Item { value: 1 }, Item { value: 2 }].iter().contain(&Item { value: 2 }));
646+
assert!([Item { value: 1i32 }, Item { value: 2i32 }].iter().contain(&Item { value: 2i32 }));
647647
}
648648

649-
// #[test]
650-
// fn test_str_iterator_contain_string() {
651-
// assert!(["a", "b", "c"].iter().contain("b".to_owned()));
652-
// assert!(!["a", "b", "c"].iter().contain("d".to_owned()));
653-
// }
649+
#[test]
650+
fn test_str_iterator_contain_string() {
651+
assert!(["a", "b", "c"].iter().contain(&"b".to_owned()));
652+
assert!(!["a", "b", "c"].iter().contain(&"d".to_owned()));
653+
}
654654

655655
#[test]
656656
fn test_str_iterator_contain_string_slice() {
@@ -660,14 +660,14 @@ fn test_str_iterator_contain_string_slice() {
660660

661661
#[test]
662662
fn test_string_iterator_contain_str_slice() {
663-
assert!(["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contain("b"));
664-
assert!(!["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contain("d"));
663+
assert!(["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contain(&"b"));
664+
assert!(!["a".to_owned(), "b".to_owned(), "c".to_owned()].iter().contain(&"d"));
665665
}
666666

667667

668668
#[test]
669669
fn test_edge_case_handling_iterator_with_large_number_of_elements() {
670-
assert!((1..1000).contain(500));
670+
assert!((1..1000).contain(500i32));
671671
}
672672

673673

0 commit comments

Comments
 (0)