]> git.lizzy.rs Git - rust.git/commitdiff
Added remove_from to vec.rs
authormadseagames <madseagames@gmail.com>
Sat, 3 Dec 2016 16:47:27 +0000 (12:47 -0400)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Tue, 7 Mar 2017 17:44:51 +0000 (19:44 +0200)
src/libcollections/vec.rs

index 3134e3c2ce12f802f56016d207317265208d0fb7..d38c9f6e1cf805f3a82b250e6afeb23e5e4f1f27 100644 (file)
@@ -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))
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////