File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -784,6 +784,26 @@ where
784784 }
785785 }
786786 }
787+ /// Removes an element from the vector and returns it.
788+ ///
789+ /// The removed element is replaced by the last element of the vector.
790+ ///
791+ /// This does not preserve ordering of the remaining elements, but is *O*(1).
792+ /// If you need to preserve the element order, use [`remove`] instead.
793+ pub fn swap_remove ( & mut self , index : usize ) -> T {
794+ if index > self . len ( ) {
795+ panic ! ( "Index out of range" ) ;
796+ }
797+ // SAFETY: index is in range
798+ // self.len() - 1 is in range since at last 1 element exists
799+ unsafe {
800+ let old = ptr:: read ( self . as_ptr ( ) . add ( index) ) ;
801+ let last = ptr:: read ( self . as_ptr ( ) . add ( self . len ( ) - 1 ) ) ;
802+ ptr:: write ( self . as_mut_ptr ( ) . add ( index) , last) ;
803+ self . dec_len ( 1 ) ;
804+ old
805+ }
806+ }
787807}
788808
789809impl < T : Clone , A : Allocator > Vec < T , A > {
You can’t perform that action at this time.
0 commit comments