diff options
| author | Ariel Ben-Yehuda <arielb1@mail.tau.ac.il> | 2017-03-08 20:54:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-08 20:54:05 +0200 |
| commit | 99aad021ce826b1e3ea83dea31f7014e00f56411 (patch) | |
| tree | 8c0040e3b9c0a19504f2d9e6bd65b0cc41d944c6 | |
| parent | 4e347d634d64b1f346d5bd2cefdaed4b5e86f090 (diff) | |
| parent | df617195f06c27821803acc87e0b1b0dd288799a (diff) | |
| download | rust-99aad021ce826b1e3ea83dea31f7014e00f56411.tar.gz rust-99aad021ce826b1e3ea83dea31f7014e00f56411.zip | |
Rollup merge of #40325 - eddyb:pr38143, r=alexcrichton
Added remove_from to vec.rs (#38143) Turns out that if you push to someone's PR branch and cause the PR to close, you lose delegation 😞. @madseagames I'm really sorry about that ðŸ˜
| -rw-r--r-- | src/libcollections/vec.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 3134e3c2ce1..d38c9f6e1cf 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -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)) + } } //////////////////////////////////////////////////////////////////////////////// |
