Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Collections

Rust Vec extend, append and split_off

Reference for Vec::extend, extend_from_slice, append, split_off, splice and concat in Rust: moving versus cloning, ownership and complexity.

Rust offers several ways to join vectors and to cut one in two. extend appends everything an iterator produces. extend_from_slice clones elements from a slice. append moves all elements out of another vector, leaving it empty. split_off(at) does the opposite of joining: it returns a new vector holding the tail.

Which one you choose depends on ownership. If the source is a vector you no longer need, append or extend(other) moves items without cloning. If you only borrow the source, extend_from_slice clones.

Rust
let mut a = vec![1, 2];
a.extend([3, 4]);
a.extend_from_slice(&[5, 6]);

Try it

Rust extend, append and extend_from_slice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust split_off and splice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
impl<T> Vec<T> {
    pub fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I);   // trait Extend
    pub fn extend_from_slice(&mut self, other: &[T]) where T: Clone;
    pub fn append(&mut self, other: &mut Vec<T>);
    pub fn split_off(&mut self, at: usize) -> Vec<T>;
    pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter>;
}
Name Description
iter Any iterator or collection. Items are appended in order.
other The elements to add. For append it is emptied.
at The split position: the original keeps [0, at) and the result holds [at, len).
range, replace_with For splice, the range to remove and the items to put in its place.

Returns: split_off returns the new tail vector. splice returns an iterator over the removed elements. The others return nothing.

Panics: split_off panics when at > len. splice panics for an invalid range.

Usage notes

  • extend accepts &T for T: Copy. That is why extend(other.iter()) works for numbers but not for String (use .cloned() or .iter().cloned() there).
  • append keeps the capacity of other, so you can reuse the emptied vector as a scratch buffer.
  • extend reserves ahead using the iterator's size_hint, so extending from an exact-size iterator allocates at most once.
  • Joining slices of vectors: [a, b].concat() builds a new Vec by cloning; .join(&sep) inserts a separator element between parts.
  • splice is lazy: the replacement happens when the returned iterator is dropped. Ignoring the return value is fine, but you must not leak it.
  • split_off allocates a new vector and moves the tail. To just discard the tail use truncate. To split a borrowed slice use split_at.
  • Self-extension: v.extend(v.clone()) works, but v.extend(&v) does not compile (a shared and an exclusive borrow overlap). Use v.extend_from_within(..) to duplicate a range in place.
  • Building with collect is often clearer than a loop of push calls; see collect.