Hello! Today, we’re going to delve into one of Rust’s most versatile and powerful data structures — vectors. Just as we explored arrays in our previous lesson, vectors also store a collection of elements of the same type. However, unlike arrays, vectors are dynamic and can grow and shrink as needed.
In this lesson, we'll cover the essentials of creating, modifying, and managing vectors in Rust. We’ll look into different ways of creating vectors, adding and removing elements, and understanding how Rust handles data and ownership within vectors. By the end of this lesson, you'll have a strong grasp of vectors and be ready to use them effectively in your Rust programs.
Let's get started!
Vectors can be created in Rust with or without specifying the data type explicitly. If the type is not explicitly mentioned, Rust will infer it based on the values pushed into the vector. To declare a new vector explicitly use Vec
followed by the data type within <>
. To add new elements to a vector, use push
to append the new value to the end of the vector. To implicitly declare a vector, use vec!
followed by the elements inside brackets.
Here are a couple of examples to illustrate this:
In this example:
vector_with_type
is explicitly typed as a vector ofi32
values. Elements are pushed into the vector using thepush
method.vector_without_type
uses type inference, determining the type from the initial values provided.
You can access elements of a vector using both the get
method and direct indexing. The get
method returns an Option
type that can be used to handle out-of-bounds errors gracefully. The get
method returns an Option<&T>
where T
is the type of the elements in the vector. The Option
type can be Some(&element)
if the index is valid, or None
if the index is out of bounds.
To ensure the valid access of an element, use the pattern matching construct if let Some(&element) = vector.get(index)
. If index
is indeed a valid index, element
takes on the value of the element in the vector, and the if
block is executed. If index
is not a valid index, element
takes on the value of None
, and the if
block does not execute.
- If 0 is a valid index of
vector
(it is),vector.get(0)
returnsSome(&first_elem)
and bindsfirst_elem
to the value of the first element ofvector
. - The
if
block is executed becauseSome(first_elem)
is notNone
. vector[1]
directly accesses the second element but can panic if the index is out-of-bounds.
Vectors in Rust are immutable by default, but can be made mutable using the mut
keyword. This allows you to modify their elements and change their size:
We created a mutable vector mutable_vector
then modified the second element using mutable_vector[1] = 42
.
Rust provides methods like pop
and remove
to remove elements from a vector, simplifying management tasks.
.pop
returns the last element of the vector and removes it from the vector.
The remove
method is used to remove an element from a vector at a specified index. This operation shifts all elements after the specified index one position to the left, effectively reducing the vector's length by one. The method also returns the removed element.
vector.pop()
removes the last element and returns it wrapped inSome
, orNone
if the vector is empty.vector.remove(1)
removes and returns the element at index 1. If the index is out-of-bounds, this code will cause an error during runtime.
Handling ownership and the concept of copy versus non-copy data types is crucial in Rust. Vectors themselves are not Copy
types in Rust. Even if the elements inside the vector are of a type that implements the Copy
trait (such as i32
), the vector itself does not implement the Copy
trait. Like arrays, using direct indexing on a vector (vector[index]
) does not move ownership of a non-copy element. You can only create a reference to the element. Here's how it works with vectors:
Vector with Copy Data
vector_with_copy
containsi32
elements, which are copy types.vector_with_copy[0]
accesses the first element,1
, which is of typei32
.first_elem_copy
is assigned the value1
. Sincei32
implements theCopy
trait, the value is copied rather than moved.
Vector with Non-Copy Data
vector_with_non_copy
is initialized withString
elements.String
does not implement theCopy
trait.- The line
&vector_with_non_copy[0]
creates a reference to the first element of the vector - The line
vector_with_non_copy[0]
causes an error because you cannot move ownership of elements in a non-Copy vector
Ownership Transfer
- Ownership of
vector_with_copy
is moved tocopied_vector
, makingvector_with_copy
invalid.
Slices allow you to work with portions of a vector without creating a new one. To create a vector slice, use a reference to the vector followed the starting index up to, but not including the ending index. To create a full slice of an vector, simply place ..
inside the brackets. Here’s how you can create and use slices:
&vector[1..3]
creates an immutable slice of the vector.&mut vector_for_slice[1..3]
creates a mutable slice, allowing modifications to the vector's elements.
Vectors can be passed to functions by reference or by value, affecting ownership and data access. The rules for passing vectors to functions are as follows:
- Passing a reference to an vector does not transfer ownership.
- Unlike arrays, passing a vector by value always transfers ownership, regardless of whether the elements implement the
Copy
trait or not.
In this code:
-
The function
display_vector_reference
takes a reference to a vector with twoString
elements. This allows the function to read the vector without taking ownership, so the vector remains available in the main function. -
The function
display_vector_ownership
takes ownership of a vector with twoi32
elements. This consumes the vector, making it unavailable for further use in the main function. The last print statement inmain
causes an error because the vector's ownership has been moved to the function.
Congratulations! You have just taken a significant step in mastering vectors in Rust. We have covered how to create, modify, and access vectors, slice them, and understand the importance of data ownership and copy types within vectors.
With vectors, you've unlocked a dynamic and powerful way to manage collections in Rust. They provide flexibility and efficiency that’s easy to integrate into any Rust project. As you continue to practice, keep experimenting with different ways to manipulate and use vectors.
Now it's time to put your newfound skills to the test. Dive into the exercises ahead and enjoy the hands-on practice. Your journey into the world of Rust is getting even more exciting. Happy coding!
