@@ -463,7 +463,7 @@ where
463463 /// If `new_len` is greater than len, the Vec is extended by the difference,
464464 /// with each additional slot filled with `value`.
465465 /// If `new_len` is less than len, the Vec is simply truncated.
466- fn resize ( & mut self , new_len : usize , value : T , flags : Flags ) -> Result < ( ) , AllocError >
466+ pub fn resize ( & mut self , new_len : usize , value : T , flags : Flags ) -> Result < ( ) , AllocError >
467467 where
468468 T : Clone ,
469469 {
@@ -628,6 +628,26 @@ where
628628 }
629629 }
630630 }
631+ /// Removes an element from the vector and returns it.
632+ ///
633+ /// The removed element is replaced by the last element of the vector.
634+ ///
635+ /// This does not preserve ordering of the remaining elements, but is *O*(1).
636+ /// If you need to preserve the element order, use [`remove`] instead.
637+ pub fn swap_remove ( & mut self , index : usize ) -> T {
638+ if index > self . len ( ) {
639+ panic ! ( "Index out of range" ) ;
640+ }
641+ // SAFETY: index is in range
642+ // self.len() - 1 is in range since at last 1 element exists
643+ unsafe {
644+ let old = ptr:: read ( self . as_ptr ( ) . add ( index) ) ;
645+ let last = ptr:: read ( self . as_ptr ( ) . add ( self . len ( ) - 1 ) ) ;
646+ ptr:: write ( self . as_mut_ptr ( ) . add ( index) , last) ;
647+ self . set_len ( self . len - 1 ) ;
648+ old
649+ }
650+ }
631651}
632652
633653impl < T : Clone , A : Allocator > Vec < T , A > {
0 commit comments