May 2019
Intermediate to advanced
698 pages
17h 21m
English
Vec<T> and VecDeque<T> both remain efficient when removing items. Although, they don't change the amount of memory allocated to the data structure, both types provide a function called shrink_to_fit() to readjust the capacity to the length it has.
On remove, Vec<T> shifts the remaining elements toward the start of the sequence. Like the insert() function, it simply copies the entire remaining data with an offset, shown as follows:
#[stable(feature = "rust1", since = "1.0.0")]pub fn remove(&mut self, index: usize) -> T { let len = self.len(); assert!(index < len); unsafe { // infallible let ret; { // the place we are taking from. let ptr = self.as_mut_ptr().add(index); // copy it out, unsafely having a copy of the value on // the stack ...Read now
Unlock full access