Skip to content

Commit 74229a8

Browse files
authored
Rollup merge of rust-lang#40325 - eddyb:pr38143, r=alexcrichton
Added remove_from to vec.rs (rust-lang#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 😭
2 parents a75fe45 + df61719 commit 74229a8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

β€Žsrc/libcollections/vec.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,27 @@ impl<T: PartialEq> Vec<T> {
13351335
pub fn dedup(&mut self) {
13361336
self.dedup_by(|a, b| a == b)
13371337
}
1338+
1339+
/// Removes the first instance of `item` from the vector if the item exists.
1340+
///
1341+
/// # Examples
1342+
///
1343+
/// ```
1344+
///# #![feature(vec_remove_item)]
1345+
/// let mut vec = vec![1, 2, 3, 1];
1346+
///
1347+
/// vec.remove_item(&1);
1348+
///
1349+
/// assert_eq!(vec, vec![2, 3, 1]);
1350+
/// ```
1351+
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
1352+
pub fn remove_item(&mut self, item: &T) -> Option<T> {
1353+
let pos = match self.iter().position(|x| *x == *item) {
1354+
Some(x) => x,
1355+
None => return None,
1356+
};
1357+
Some(self.remove(pos))
1358+
}
13381359
}
13391360

13401361
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
Β (0)