File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -799,6 +799,26 @@ where
799799 }
800800 }
801801 }
802+ /// Removes an element from the vector and returns it.
803+ ///
804+ /// The removed element is replaced by the last element of the vector.
805+ ///
806+ /// This does not preserve ordering of the remaining elements, but is *O*(1).
807+ /// If you need to preserve the element order, use [`remove`] instead.
808+ pub fn swap_remove ( & mut self , index : usize ) -> T {
809+ if index > self . len ( ) {
810+ panic ! ( "Index out of range" ) ;
811+ }
812+ // SAFETY: index is in range
813+ // self.len() - 1 is in range since at last 1 element exists
814+ unsafe {
815+ let old = ptr:: read ( self . as_ptr ( ) . add ( index) ) ;
816+ let last = ptr:: read ( self . as_ptr ( ) . add ( self . len ( ) - 1 ) ) ;
817+ ptr:: write ( self . as_mut_ptr ( ) . add ( index) , last) ;
818+ self . dec_len ( 1 ) ;
819+ old
820+ }
821+ }
802822}
803823
804824impl < T : Clone , A : Allocator > Vec < T , A > {
You can’t perform that action at this time.
0 commit comments