Add contains to VecDeque and LinkedList (+ tests)

This commit is contained in:
Lukas Kalbertodt 2016-04-14 16:07:53 +02:00
parent 28c9fdafc0
commit bf3aefeba0
5 changed files with 49 additions and 0 deletions

View file

@ -401,6 +401,16 @@ impl<T> LinkedList<T> {
*self = LinkedList::new()
}
/// Returns `true` if the `LinkedList` contains an element equal to the
/// given value.
#[unstable(feature = "linked_list_contains", reason = "recently added",
issue = "32630")]
pub fn contains(&self, x: &T) -> bool
where T: PartialEq<T>
{
self.iter().any(|e| e == x)
}
/// Provides a reference to the front element, or `None` if the list is
/// empty.
///

View file

@ -872,6 +872,17 @@ impl<T> VecDeque<T> {
self.drain(..);
}
/// Returns `true` if the `VecDeque` contains an element equal to the
/// given value.
#[unstable(feature = "vec_deque_contains", reason = "recently added",
issue = "32630")]
pub fn contains(&self, x: &T) -> bool
where T: PartialEq<T>
{
let (a, b) = self.as_slices();
a.contains(x) || b.contains(x)
}
/// Provides a reference to the front element, or `None` if the sequence is
/// empty.
///