@@ -1444,6 +1444,34 @@ impl<T, A: Allocator> Vec<T, A> {
14441444 pub fn retain < F > ( & mut self , mut f : F )
14451445 where
14461446 F : FnMut ( & T ) -> bool ,
1447+ {
1448+ self . retain_mut ( |elem| f ( elem) ) ;
1449+ }
1450+
1451+ /// Retains only the elements specified by the predicate, passing a mutable reference to it.
1452+ ///
1453+ /// In other words, remove all elements `e` such that `f(&mut e)` returns `false`.
1454+ /// This method operates in place, visiting each element exactly once in the
1455+ /// original order, and preserves the order of the retained elements.
1456+ ///
1457+ /// # Examples
1458+ ///
1459+ /// ```
1460+ /// #![feature(vec_retain_mut)]
1461+ ///
1462+ /// let mut vec = vec![1, 2, 3, 4];
1463+ /// vec.retain_mut(|x| if *x > 3 {
1464+ /// false
1465+ /// } else {
1466+ /// *x += 1;
1467+ /// true
1468+ /// });
1469+ /// assert_eq!(vec, [2, 3, 4]);
1470+ /// ```
1471+ #[ unstable( feature = "vec_retain_mut" , issue = "90829" ) ]
1472+ pub fn retain_mut < F > ( & mut self , mut f : F )
1473+ where
1474+ F : FnMut ( & mut T ) -> bool ,
14471475 {
14481476 let original_len = self . len ( ) ;
14491477 // Avoid double drop if the drop guard is not executed,
@@ -1496,7 +1524,7 @@ impl<T, A: Allocator> Vec<T, A> {
14961524 g : & mut BackshiftOnDrop < ' _ , T , A > ,
14971525 ) -> bool
14981526 where
1499- F : FnMut ( & T ) -> bool ,
1527+ F : FnMut ( & mut T ) -> bool ,
15001528 {
15011529 // SAFETY: Unchecked element must be valid.
15021530 let cur = unsafe { & mut * g. v . as_mut_ptr ( ) . add ( g. processed_len ) } ;
0 commit comments