← Back to Home
Category Method Example Applies To Description
Create Vec::new() let v: Vec<i32> = Vec::new(); Vec<T> Create empty vector
vec![1, 2, 3] let v = vec![1, 2, 3]; Vec<T> Macro shorthand
Vec::with_capacity(n) Vec::with_capacity(10) Vec<T> Preallocate space
Info .len() v.len() &Vec<T> Number of elements
.is_empty() v.is_empty() &Vec<T> True if no elements
.capacity() v.capacity() &Vec<T> Allocated capacity
.shrink_to_fit() v.shrink_to_fit() &mut Vec<T> Free unused capacity
Access v[i] v[0] &Vec<T> Index (panics if out of range)
.get(i) v.get(2) &Vec<T> Safe index → Option<&T>
.first() v.first() &Vec<T> First element → Option<&T>
.last() v.last() &Vec<T> Last element → Option<&T>
Add / Grow .push(value) v.push(10) &mut Vec<T> Add element to end
.insert(i, value) v.insert(1, 99) &mut Vec<T> Insert at index
.extend(iterable) v.extend(vec![4,5,6]) &mut Vec<T> Append multiple values
.append(&mut other) v.append(&mut w) &mut Vec<T> Move all items from another vec
.resize(new_len, value) v.resize(5, 0) &mut Vec<T> Grow or shrink vector
.reserve(n) v.reserve(10) &mut Vec<T> Preallocate more capacity
Remove / Shrink .pop() v.pop() &mut Vec<T> Remove last element → Option<T>
.remove(i) v.remove(2) &mut Vec<T> Remove at index
.clear() v.clear() &mut Vec<T> Remove all elements
.drain(range) v.drain(1..3) &mut Vec<T> Remove a range
.truncate(n) v.truncate(3) &mut Vec<T> Keep only first n items
Update v[i] = value v[0] = 42 &mut Vec<T> Replace element at index
.iter_mut() for x in v.iter_mut() { *x *= 2; } &mut Vec<T> Iterate mutably
Combine .extend() v.extend(vec![7,8,9]) &mut Vec<T> Append values
.append() v.append(&mut w) &mut Vec<T> Move all from another vector
[a, b].concat() [v1, v2].concat() slices Join multiple slices
Iterate .iter() for x in v.iter() &Vec<T> Iterate immutably (&T)
.iter_mut() for x in v.iter_mut() &mut Vec<T> Iterate mutably (&mut T)
.into_iter() for x in v.into_iter() Vec<T> Consume vector (T)
.for_each() `v.iter().for_each( x println!("{}", x));` &Vec<T> Run closure for each item
Filter / Transform .map() `v.iter().map( x x * 2)` &Vec<T> Transform each item
.filter() `v.iter().filter( x *x > 10)` &Vec<T> Keep matching items
.collect() .collect::<Vec<_>>() iterator Build new Vec from iterator
.cloned() v.iter().cloned().collect() &Vec<T> Clone items into new Vec
Search / Find .contains(&x) v.contains(&3) &Vec<T> Check existence
.position() `v.iter().position( x *x == 3)` &Vec<T> Find index
.find() `v.iter().find( &&x x > 10)` &Vec<T> Find first matching
.retain() `v.retain( x *x > 0)` &mut Vec<T> Keep only matches
Sort / Reverse .sort() v.sort() &mut Vec<T> Sort ascending
.sort_by() `v.sort_by( a,b b.cmp(a))` &mut Vec<T> Custom comparison
.sort_by_key() `v.sort_by_key( x x.age)` &mut Vec<T> Sort by a field
.reverse() v.reverse() &mut Vec<T> Reverse order
Slice / View &v[start..end] &v[1..3] &Vec<T> Immutable slice
&mut v[start..end] &mut v[1..3] &mut Vec<T> Mutable slice
Clone / Copy .clone() let w = v.clone(); &Vec<T> Deep copy of vector
Consume .drain(..) v.drain(..) &mut Vec<T> Remove and return all elements
.split_off(i) let w = v.split_off(3) &mut Vec<T> Move elements after index into new Vec
Utilities .split_at(i) let (a,b) = v.split_at(2) &Vec<T> Split into two slices
.split_at_mut(i) let (a,b) = v.split_at_mut(2) &mut Vec<T> Split into two mutable slices
.dedup() v.dedup() &mut Vec<T> Remove consecutive duplicates