How to use Vectors in Rust?

Reading Time: 3 minutes

Rust has been the most loved programming language for the last five years in a row. It is a low-level statically-typed multi-paradigm programming language that’s focused on safety and performance.

Rust solves problems that C/C++ has been struggling with for a long time, such as memory errors and building concurrent programs. Due to the borrow checker, Rust can prevent data races at compile-time. Data races occur when two threads access the same memory at the same time, and they can lead to unpredictable behaviour. Thankfully, preventing undefined behaviour is all that Rust is about. Vectors are an important part of a programming language. Rust also has support for vectors.

Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type. 

Rust (programming language) - Wikipedia

Create vectors in Rust

We can create a new vector by calling the Vec::new() method. Here is an example to understand better.

let arr: Vec<i32> = Vec::new();

Since we do not know the type of elements the vector will be storing, so we use the annotation to define the type of vector.

In practical code, Rust can often infer the type of value you want to store once you insert values, so you rarely need to do the annotation.

There is another way to create a new vector. We can create a vector using the vec! macro. It allows us to create a vector using values. Here is an example.

let arr = vec![1, 2, 3];

Now we will see how we can update the values in a vector.

Updating a vector

We use the push method to insert a new element in a vector. But we need to make sure that the vector is mutable to be able to change it. Here is a code that will show how we can do this.

    let mut fruits = Vec::new();


Since we are pushing string in the vector, so the Rust compiler infers the type of vector and therefore we do not need the annotation.

Removing elements

To remove an element from the vector, we use the remove method. The remove method takes the index of the element which we want to remove. Vectors in Rust use 0 based indexing. Here is an example of how to remove the elements from a vector.

fn main() {
    let mut fruits = Vec::new();


The above code first pushes 4 string elements in a vector using the push method. Then it uses the remove method to remove the element at the 2nd index that is “Orange“. The output which we get is as follows.

["Apple", "Banana", "Orange", "Kiwi"]
["Apple", "Banana", "Kiwi"]

The element at the 2nd index which is Orange is successfully removed from the vector.

Iterating over the Values in a Vector

We can access all the elements of a vector by iterating over the vector instead of using the indices of the vector. We can use a for loop to get an immutable reference to each element of the vector.

Here is an example to show how can we iterate over the elements in a vector.

 let arr = vec![12, 42, 67];
    for item in &arr {
        println!("{}", item);

The above code uses a for loop to iterate over the vector, get an immutable reference to each element in it and then print it.

The output of the above code is given below.


This was all about the vectors in Rust. Stay tuned for more blogs.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: