How to Concatenate Vectors in Rust

1. Overview

Vector (Vec<T>) is a collection in Rust that allows storing multiple values of the same type in a single data structure. Unlike an array, Vectors can grow or shrink in size.

In this tutorial, we’ll explore different ways to concatenate Vectors in Rust.

2. Using append()

The append() method moves the content of a vector to another vector leaving the content of the source vector empty.

Here’s a sample code that uses the append() method:

let mut vector1 = vec![1, 2, 3];
let mut vector2 = vec![4, 5, 6];
vector1.append(&mut vector2);
assert_eq!(vector1, [1, 2, 3, 4, 5, 6]);
assert_eq!(vector2, []);

In the code above, we create two mutable vectors named vector1 and vector2. Next, we use the append() to move the values of vector2 to vector1. Notably, the vector must be mutable to use the append() method.

After moving vector2 to vector1, vector2 becomes empty because its values have been moved.

3. Using extend()

We can use the extend() method from Extend trait to concatenate vectors. extend() method work with an iterator, a collection can be thought of as an iterator because it produces a series of values.

Let’s see an example code on how to use extend() method:

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]);

Here, we move the content of b to a. We can’t use anywhere in the code because it has been moved to a. Additionally, we can clone vector instead of moving it to a:

let mut a = vec![1, 2, 3];
let b = vec![4, 5, 6];
a.extend(b.clone());
assert_eq!(a, [1, 2, 3, 4, 5, 6]);
assert_eq!(b, [4, 5, 6]);

Here, we modify the code to clone instead of moving it to a. We can now use if needed because its content wasn’t moved but cloned.

4. Using chain()

The chain() method helps to link two iterators together. Since a collection can also be thought of as an iterator, it can help to concatenate vectors together.

Let’s see an example code that uses chain() method:

let mut a = vec![1, 2, 3];
let b = vec![4, 5, 6];
let c: Vec<i32> = a.into_iter().chain(b.into_iter()).collect();
assert_eq!(c, [1, 2, 3, 4, 5, 6]);

Here, we create a new vector named c. Next, we invoke the into_iter() on a and b. The into_iter() method moves the ownership of a and b to c. Then, we use the chain() to concatenate a and b together and store the values in c.

Notably, a and b cannot be used anymore because they have been moved.

We can clone a and b instead of moving them:

let c: Vec = a.iter().cloned().chain(b.iter().cloned()).collect();
assert_eq!(c, [1, 2, 3, 4, 5, 6]);
assert_eq!(a, [1, 2, 3]);

Here, we clone a and instead of moving it explicitly to c.

5. Using concat()

Simply put, the concat() method can help concatenate two or more vectors together. It takes a slice of vector as an argument and returns a new vector:

let a = vec![1, 2, 3];
let b = vec![4, 5, 6];
let c = [a, b].concat();
assert_eq!(c, [1, 2, 3, 4, 5, 6]);

Here, we concatenate a and using concat(). Notably, the ownership of a and b are moved. We can either clone or copy the two vectors to maintain the ownership.

6. Conclusion

In this article, we learned how to concatenate vectors using four different methods. We also demonstrate the ownership concept in moving the data of the vectors.

The complete source code for the examples is available on GitHub.