@@ -2148,14 +2148,45 @@ impl<T, A: Allocator> VecDeque<T, A> {
2148
2148
pub fn retain < F > ( & mut self , mut f : F )
2149
2149
where
2150
2150
F : FnMut ( & T ) -> bool ,
2151
+ {
2152
+ self . retain_mut ( |elem| f ( elem) ) ;
2153
+ }
2154
+
2155
+ /// Retains only the elements specified by the predicate.
2156
+ ///
2157
+ /// In other words, remove all elements `e` such that `f(&e)` returns false.
2158
+ /// This method operates in place, visiting each element exactly once in the
2159
+ /// original order, and preserves the order of the retained elements.
2160
+ ///
2161
+ /// # Examples
2162
+ ///
2163
+ /// ```
2164
+ /// #![feature(vec_retain_mut)]
2165
+ ///
2166
+ /// use std::collections::VecDeque;
2167
+ ///
2168
+ /// let mut buf = VecDeque::new();
2169
+ /// buf.extend(1..5);
2170
+ /// buf.retain_mut(|x| if *x % 2 == 0 {
2171
+ /// *x += 1;
2172
+ /// true
2173
+ /// } else {
2174
+ /// false
2175
+ /// });
2176
+ /// assert_eq!(buf, [3, 5]);
2177
+ /// ```
2178
+ #[ unstable( feature = "vec_retain_mut" , issue = "90829" ) ]
2179
+ pub fn retain_mut < F > ( & mut self , mut f : F )
2180
+ where
2181
+ F : FnMut ( & mut T ) -> bool ,
2151
2182
{
2152
2183
let len = self . len ( ) ;
2153
2184
let mut idx = 0 ;
2154
2185
let mut cur = 0 ;
2155
2186
2156
2187
// Stage 1: All values are retained.
2157
2188
while cur < len {
2158
- if !f ( & self [ cur] ) {
2189
+ if !f ( & mut self [ cur] ) {
2159
2190
cur += 1 ;
2160
2191
break ;
2161
2192
}
@@ -2164,7 +2195,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2164
2195
}
2165
2196
// Stage 2: Swap retained value into current idx.
2166
2197
while cur < len {
2167
- if !f ( & self [ cur] ) {
2198
+ if !f ( & mut self [ cur] ) {
2168
2199
cur += 1 ;
2169
2200
continue ;
2170
2201
}
@@ -2173,7 +2204,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2173
2204
cur += 1 ;
2174
2205
idx += 1 ;
2175
2206
}
2176
- // Stage 3: Trancate all values after idx.
2207
+ // Stage 3: Truncate all values after idx.
2177
2208
if cur != idx {
2178
2209
self . truncate ( idx) ;
2179
2210
}
0 commit comments