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