Skip to main content
In this article, we dive into vectors in Rust—a versatile, dynamic array data structure that grows and shrinks as needed. Vectors are one of the most commonly used data structures in Rust since they provide efficient, heap-allocated storage for a variable amount of data. By the end of this article, you will understand how to create, update, iterate, and manipulate vectors in Rust while respecting its ownership and borrowing rules.
The image is a slide titled "Exploring Vectors," explaining that vectors in Rust are commonly used, growable, heap-allocated data structures.

Creating Vectors

There are several ways to create vectors in Rust. One straightforward method is using the Vec::new() function to generate an empty vector and then adding elements with the push method:
Alternatively, you can initialize a vector with predefined values using the vec! macro:

Updating Vectors

Vectors are dynamic, allowing you to modify them using methods like push, pop, and insert:
  • The push method appends an element to the end of the vector.
  • The pop method removes the last element and returns it (returning None if the vector is empty).
  • The insert method adds an element at a specified index, shifting other elements to the right.

Examples

When accessing elements by index, using an out-of-bound index will cause the program to panic. Instead, you can use the get method which returns an Option, allowing for safer access:

Iterating Over Vectors

There are various ways to iterate through a vector. You can use a simple for loop to iterate by reference:
Alternatively, use the iter method to obtain an iterator over references to the elements:
If you need to modify each element while iterating, use mutable references:

Common Vector Methods

Length and Empty Check

You can check the size and emptiness of a vector with the len and is_empty methods:

Checking for an Element

To verify if a vector contains a specific element, use the contains method:

Removing Elements

Remove an element at a specific index with the remove method, which also returns the removed element:

Extending a Vector

Append elements from any iterator to the end of a vector using the extend method:

Ownership and Borrowing

Rust’s ownership and borrowing rules apply to vectors too. When a vector is assigned to a new variable, ownership is transferred (moved), and the original variable becomes invalid. For instance:
To work with vectors without transferring ownership, borrow the vector or its elements using references:

Borrowing an Entire Vector in a Function

When passing a vector to a function without transferring ownership, use a reference parameter. This way, the original vector remains valid after the function call:
Understanding Rust’s ownership and borrowing model is crucial when working with vectors. It helps prevent common bugs related to memory safety.
This concludes our exploration of vectors in Rust. By mastering the creation, updating, iteration, and manipulation of vectors—and by understanding Rust’s ownership and borrowing rules—you can unlock the full potential of this flexible data structure in your Rust programs.

Further Reading

Watch Video