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.
let mut a = vec![1, 2]; a.extend([3, 4]); a.extend_from_slice(&[5, 6]);
Try it
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.
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
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
extendaccepts&TforT: Copy. That is whyextend(other.iter())works for numbers but not forString(use.cloned()or.iter().cloned()there).appendkeeps the capacity ofother, so you can reuse the emptied vector as a scratch buffer.extendreserves ahead using the iterator'ssize_hint, so extending from an exact-size iterator allocates at most once.- Joining slices of vectors:
[a, b].concat()builds a newVecby cloning;.join(&sep)inserts a separator element between parts. spliceis lazy: the replacement happens when the returned iterator is dropped. Ignoring the return value is fine, but you must not leak it.split_offallocates a new vector and moves the tail. To just discard the tail usetruncate. To split a borrowed slice usesplit_at.- Self-extension:
v.extend(v.clone())works, butv.extend(&v)does not compile (a shared and an exclusive borrow overlap). Usev.extend_from_within(..)to duplicate a range in place. - Building with
collectis often clearer than a loop ofpushcalls; seecollect.