1
Fork 0

add partial eq bound to remove_item

This commit is contained in:
dylan_DPC 2020-01-04 23:31:32 +05:30
parent abf2e00e38
commit c09dac1073

View file

@ -1688,6 +1688,9 @@ impl<T: PartialEq> Vec<T> {
pub fn dedup(&mut self) {
self.dedup_by(|a, b| a == b)
}
}
impl <T> Vec<T> {
/// Removes the first instance of `item` from the vector if the item exists.
///
@ -1701,11 +1704,15 @@ impl<T: PartialEq> Vec<T> {
///
/// assert_eq!(vec, vec![2, 3, 1]);
/// ```
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
pub fn remove_item(&mut self, item: &T) -> Option<T> {
pub fn remove_item<V>(&mut self, item: &V) -> Option<T>
where T: PartialEq<V>
{
let pos = self.iter().position(|x| *x == *item)?;
Some(self.remove(pos))
}
}
////////////////////////////////////////////////////////////////////////////////