@@ -1444,6 +1444,34 @@ impl<T, A: Allocator> Vec<T, A> {
1444
1444
pub fn retain < F > ( & mut self , mut f : F )
1445
1445
where
1446
1446
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 ,
1447
1475
{
1448
1476
let original_len = self . len ( ) ;
1449
1477
// Avoid double drop if the drop guard is not executed,
@@ -1496,7 +1524,7 @@ impl<T, A: Allocator> Vec<T, A> {
1496
1524
g : & mut BackshiftOnDrop < ' _ , T , A > ,
1497
1525
) -> bool
1498
1526
where
1499
- F : FnMut ( & T ) -> bool ,
1527
+ F : FnMut ( & mut T ) -> bool ,
1500
1528
{
1501
1529
// SAFETY: Unchecked element must be valid.
1502
1530
let cur = unsafe { & mut * g. v . as_mut_ptr ( ) . add ( g. processed_len ) } ;
0 commit comments