Skip to content

Commit 133f60f

Browse files
committed
Auto merge of #32951 - LukasKalbertodt:collection_contains_rfc1552, r=brson
Add `contains` to `VecDeque` and `LinkedList` (+ tests) This implements [RFC 1552](https://github.com/rust-lang/rfcs/blob/master/text/1552-contains-method-for-various-collections.md). Tracking issue: #32630 Sorry for the late response. This is my first contribution, so please tell me if anything isn't optimal!
2 parents 9bba290 + bf3aefe commit 133f60f

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

src/libcollections/linked_list.rs

+10
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,16 @@ impl<T> LinkedList<T> {
403403
*self = LinkedList::new()
404404
}
405405

406+
/// Returns `true` if the `LinkedList` contains an element equal to the
407+
/// given value.
408+
#[unstable(feature = "linked_list_contains", reason = "recently added",
409+
issue = "32630")]
410+
pub fn contains(&self, x: &T) -> bool
411+
where T: PartialEq<T>
412+
{
413+
self.iter().any(|e| e == x)
414+
}
415+
406416
/// Provides a reference to the front element, or `None` if the list is
407417
/// empty.
408418
///

src/libcollections/vec_deque.rs

+11
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,17 @@ impl<T> VecDeque<T> {
873873
self.drain(..);
874874
}
875875

876+
/// Returns `true` if the `VecDeque` contains an element equal to the
877+
/// given value.
878+
#[unstable(feature = "vec_deque_contains", reason = "recently added",
879+
issue = "32630")]
880+
pub fn contains(&self, x: &T) -> bool
881+
where T: PartialEq<T>
882+
{
883+
let (a, b) = self.as_slices();
884+
a.contains(x) || b.contains(x)
885+
}
886+
876887
/// Provides a reference to the front element, or `None` if the sequence is
877888
/// empty.
878889
///

src/libcollectionstest/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(fn_traits)]
2121
#![feature(enumset)]
2222
#![feature(iter_arith)]
23+
#![feature(linked_list_contains)]
2324
#![feature(map_entry_keys)]
2425
#![feature(map_values_mut)]
2526
#![feature(pattern)]
@@ -30,6 +31,7 @@
3031
#![feature(test)]
3132
#![feature(unboxed_closures)]
3233
#![feature(unicode)]
34+
#![feature(vec_deque_contains)]
3335

3436
extern crate collections;
3537
extern crate test;

src/libcollectionstest/linked_list.rs

+13
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,16 @@ fn bench_iter_mut_rev(b: &mut test::Bencher) {
429429
assert!(m.iter_mut().rev().count() == 128);
430430
})
431431
}
432+
433+
#[test]
434+
fn test_contains() {
435+
let mut l = LinkedList::new();
436+
l.extend(&[2, 3, 4]);
437+
438+
assert!(l.contains(&3));
439+
assert!(!l.contains(&1));
440+
441+
l.clear();
442+
443+
assert!(!l.contains(&3));
444+
}

src/libcollectionstest/vec_deque.rs

+13
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,16 @@ fn test_extend_ref() {
959959
assert_eq!(v[4], 5);
960960
assert_eq!(v[5], 6);
961961
}
962+
963+
#[test]
964+
fn test_contains() {
965+
let mut v = VecDeque::new();
966+
v.extend(&[2, 3, 4]);
967+
968+
assert!(v.contains(&3));
969+
assert!(!v.contains(&1));
970+
971+
v.clear();
972+
973+
assert!(!v.contains(&3));
974+
}

0 commit comments

Comments
 (0)