The std::vec::Vec structure has an append() method:
fn append(&mut self, other: &mut Vec<T>)
Moves all other elements to Self , leaving other empty.
In your example, the following code combines two vectors with mutating a and b :
fn main() { let mut a = vec![1, 2, 3]; let mut b = vec![4, 5, 6]; a.append(&mut b); assert_eq!(a, [1, 2, 3, 4, 5, 6]); assert_eq!(b, []); }
Alternatively, you can use Extend::extend() to add all the elements of what can be turned into an iterator (like Vec ) so that the given vector is:
let mut a = vec![1, 2, 3]; let b = vec![4, 5, 6]; a.extend(b); assert_eq!(a, [1, 2, 3, 4, 5, 6]);
Note that the vector b moving, not empty. If your vectors contain elements that implement Copy , you can pass an immutable link to one vector to extend() instead to avoid moving. In this case, vector b does not change:
let mut a = vec![1, 2, 3]; let b = vec![4, 5, 6]; a.extend(&b); assert_eq!(a, [1, 2, 3, 4, 5, 6]); assert_eq!(b, [4, 5, 6]);