Vec::push() / pop() / insert() / remove()
Rust's Vec provides a rich set of methods for adding and removing elements. Use push() to append to the end, pop() to retrieve and remove the last element, insert() to insert at an arbitrary position, and remove() to delete at an arbitrary position.
Syntax
let mut v = vec![1, 2, 3]; // Appends an element to the end. v.push(4); // [1, 2, 3, 4] // Removes and returns the last element (returns Option<T>). let last = v.pop(); // Some(4), v = [1, 2, 3] // Inserts an element at the specified index. v.insert(1, 99); // [1, 99, 2, 3] // Removes and returns the element at the specified index. let removed = v.remove(1); // 99, v = [1, 2, 3] // Removes all elements that do not satisfy the condition (in-place). v.retain(|&x| x % 2 != 0); // Keeps only odd numbers. // Removes all elements (capacity is preserved). v.clear();
Method List
| Method | Description |
|---|---|
| v.push(x) | Appends element x to the end. |
| v.pop() | Removes and returns the last element as Option<T>. |
| v.insert(i, x) | Inserts element x at index i. |
| v.remove(i) | Removes and returns the element at index i. |
| v.swap_remove(i) | Swaps the element at index i with the last element and removes it (fast). |
| v.retain(|x| condition) | Removes all elements that do not satisfy the condition (in-place). |
| v.retain_mut(|x| condition) | Removes elements while evaluating the condition via a mutable reference. |
| v.truncate(n) | Removes all elements after index n. |
| v.clear() | Removes all elements (capacity is preserved). |
| v.drain(range) | Removes elements in the specified range and returns them as an iterator. |
| v.append(&mut other) | Moves all elements from other into v (other becomes empty). |
Sample Code
fn main() {
let mut v = vec![10, 20, 30, 40, 50];
println!("Initial: {:?}", v);
// push (append to end).
v.push(60);
v.push(70);
println!("push(60), push(70): {:?}", v);
// pop (remove from end).
let last = v.pop();
println!("pop() = {:?}, v = {:?}", last, v);
// insert (insert at arbitrary position).
v.insert(2, 99); // Inserts 99 at index 2.
println!("insert(2, 99): {:?}", v);
// remove (delete at arbitrary position).
let removed = v.remove(2);
println!("remove(2) = {}, v = {:?}", removed, v);
// swap_remove (fast removal: swaps with the last element).
// The order changes, but it is faster than remove.
let swapped = v.swap_remove(1);
println!("swap_remove(1) = {}, v = {:?}", swapped, v);
println!();
// retain (filter by condition).
let mut nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
nums.retain(|&x| x % 2 == 0); // Keeps only even numbers.
println!("retain even numbers: {:?}", nums);
// truncate (remove elements from index n onward).
let mut v = vec![1, 2, 3, 4, 5];
v.truncate(3);
println!("truncate(3): {:?}", v);
// drain (extract and remove a range).
let mut v = vec![1, 2, 3, 4, 5];
let drained: Vec<i32> = v.drain(1..3).collect(); // Extracts elements at index 1 and 2.
println!("drain(1..3): drained={:?}, v={:?}", drained, v);
// append (move all elements from another Vec).
let mut v1 = vec![1, 2, 3];
let mut v2 = vec![4, 5, 6];
v1.append(&mut v2);
println!("after append: v1={:?}, v2={:?}", v1, v2); // v2 becomes empty.
// clear (remove all elements).
let mut v = vec![1, 2, 3];
let cap_before = v.capacity();
v.clear();
println!("after clear: len={}, capacity={}", v.len(), v.capacity());
println!("capacity preserved: {} → {}", cap_before, v.capacity());
}
Notes
remove() has O(n) time complexity because it shifts all subsequent elements forward by one. If you do not need to preserve the order, use swap_remove() instead, which deletes in O(1).
retain() is a convenient method for removing elements during iteration. Internally it compacts only the elements that satisfy the condition toward the front, so it runs in O(n).
pop() returns Option<T>. It returns None when the Vec is empty, so handle the return value with match or unwrap_or() as needed.
For creating a Vec, see Vec::new() / vec![]. For sorting and deduplication, see Vec::sort() / dedup() / extend().
If you find any errors or copyright issues, please contact us.