We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 295768a commit 6faa295Copy full SHA for 6faa295
src/liballoc/vec.rs
@@ -810,11 +810,13 @@ impl<T> Vec<T> {
810
#[stable(feature = "rust1", since = "1.0.0")]
811
pub fn swap_remove(&mut self, index: usize) -> T {
812
unsafe {
813
- // We replace self[index] with the last element. Note that this is
814
- // safe even when index == self.len() - 1, as pop() only uses
815
- // ptr::read and leaves the memory at self[index] untouched.
+ // We replace self[index] with the last element. Note that if the
+ // bounds check on hole succeeds there must be a last element (which
+ // can be self[index] itself).
816
let hole: *mut T = &mut self[index];
817
- ptr::replace(hole, self.pop().unwrap())
+ let last = ptr::read(self.get_unchecked(self.len - 1));
818
+ self.len -= 1;
819
+ ptr::replace(hole, last)
820
}
821
822
0 commit comments