Skip to content

Commit 580f728

Browse files
committed
updates retain on vec
1 parent b0ae04c commit 580f728

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![feature(alloc_layout_extra)]
126126
#![feature(try_trait)]
127127
#![feature(associated_type_bounds)]
128+
#![feature(slice_retain)]
128129

129130
// Allow testing this library
130131

src/liballoc/vec.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -1103,26 +1103,12 @@ impl<T> Vec<T> {
11031103
/// assert_eq!(vec, [2, 3, 5]);
11041104
/// ```
11051105
#[stable(feature = "rust1", since = "1.0.0")]
1106-
pub fn retain<F>(&mut self, mut f: F)
1106+
pub fn retain<F>(&mut self, f: F)
11071107
where
11081108
F: FnMut(&T) -> bool,
11091109
{
1110-
let len = self.len();
1111-
let mut del = 0;
1112-
{
1113-
let v = &mut **self;
1114-
1115-
for i in 0..len {
1116-
if !f(&v[i]) {
1117-
del += 1;
1118-
} else if del > 0 {
1119-
v.swap(i - del, i);
1120-
}
1121-
}
1122-
}
1123-
if del > 0 {
1124-
self.truncate(len - del);
1125-
}
1110+
let retained_len = self.as_mut_slice().retain(f).len();
1111+
self.truncate(retained_len);
11261112
}
11271113

11281114
/// Removes all but the first of consecutive elements in the vector that resolve to the same

0 commit comments

Comments
 (0)