Skip to content

Commit bf3aefe

Browse files
Add contains to VecDeque and LinkedList (+ tests)
1 parent 28c9fda commit bf3aefe

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
@@ -401,6 +401,16 @@ impl<T> LinkedList<T> {
401401
*self = LinkedList::new()
402402
}
403403

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

src/libcollections/vec_deque.rs

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

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

src/libcollectionstest/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(fn_traits)]
2222
#![feature(enumset)]
2323
#![feature(iter_arith)]
24+
#![feature(linked_list_contains)]
2425
#![feature(map_entry_keys)]
2526
#![feature(map_values_mut)]
2627
#![feature(pattern)]
@@ -32,6 +33,7 @@
3233
#![feature(test)]
3334
#![feature(unboxed_closures)]
3435
#![feature(unicode)]
36+
#![feature(vec_deque_contains)]
3537

3638
extern crate collections;
3739
extern crate test;

src/libcollectionstest/linked_list.rs

+13
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,16 @@ fn bench_iter_mut_rev(b: &mut test::Bencher) {
413413
assert!(m.iter_mut().rev().count() == 128);
414414
})
415415
}
416+
417+
#[test]
418+
fn test_contains() {
419+
let mut l = LinkedList::new();
420+
l.extend(&[2, 3, 4]);
421+
422+
assert!(l.contains(&3));
423+
assert!(!l.contains(&1));
424+
425+
l.clear();
426+
427+
assert!(!l.contains(&3));
428+
}

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)