Skip to content

Commit

Permalink
Added remove_from to vec.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
madseagames authored and eddyb committed Mar 7, 2017
1 parent b04ebef commit df61719
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,27 @@ impl<T: PartialEq> Vec<T> {
pub fn dedup(&mut self) {
self.dedup_by(|a, b| a == b)
}

/// Removes the first instance of `item` from the vector if the item exists.
///
/// # Examples
///
/// ```
///# #![feature(vec_remove_item)]
/// let mut vec = vec![1, 2, 3, 1];
///
/// vec.remove_item(&1);
///
/// 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> {
let pos = match self.iter().position(|x| *x == *item) {
Some(x) => x,
None => return None,
};
Some(self.remove(pos))
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit df61719

Please sign in to comment.