A vector is a collection of similar data types of elements that are stored in a contiguous memory block. One of the most common tasks when working with vectors is slicing. Slicing refers to creating a new vector which contains only a subset of the original’s vector elements.
In this tutorial, we will learn how to perform the vector slicing using various techniques in the Rust programming language.
Rust Vectors
In Rust, we can define a vector using the vec! macro as demonstrated in the following example:
This should create a new vector called “numbers” and hold five integer values.
Another way to create vectors is by defining an empty vector first and then adding the elements as needed using the push() method.
An example is as follows:
fruits.push(1);
fruits.push(2);
fruits.push(3);
NOTE: Since we are modifying the vector after the initial declaration, we need to ensure that the value is mutable using the “mut” keyword. This allows us to write into the vector elements.
Another way to define a vector in Rust is using the with_capacity method. An example is as follows:
The provided example code should create a new vector with an initial capacity of 10. This method can be beneficial if you approximately know the total number of elements that you wish to store in the vector.
Vector Slicing
Let us now discuss how to perform the vector slicing in Rust. Consider an example vector which holds the numerical values as shown in the following:
We can extract a slice of the vector that only contains the second, third, and fourth elements using vector slicing.
We can do this by specifying the index as:
In the previous example, we define the start index as index 1. This denotes the second element in the vector since the elements in a Rust vector are zero-based.
The second value inside the square brackets defines the last index of the element that we wish to fetch. Using the range operator, it also tells the compiler that we want to include all elements from the specified index to the last selected index. In this case, it should return the second and fourth elements.
NOTE: We create a slice using a reference to the original vector. Hence, the new slice that we produce does not necessarily own the data. Therefore, any changes made in the initial vector bubbles down to the slice that is created from it.
Omit the Start Index
We can also skip the start index and only specify the target stop index. This forces Rust to grab all the elements from the start index to the specified last position.
Take the following example code:
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let slice = &numbers[..5];
println!("{:?}", slice);
}
This should return the elements from index 0 to index 5 as specified.
Omit the Stop Index
Similarly, we can omit the stop index as shown in the following example code:
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let slice = &numbers[5..];
println!("{:?}", slice);
}
The previous code should return all the elements from index 5 to the last element in the array.
Omit Both Start and Stop Index
Although not important, you can omit both the start and stop index which will fetch all the elements in the vector. This is similar to creating a reference to the vector or a copy of the original vector.
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let slice = &numbers[..];
println!("{:?}", slice);
}
This should return all elements from the original vector.
Conclusion
We covered the basics of vector slicing in Rust. We learned how to create a new vector that contains a subset of the original vector’s elements using Rust’s slice notation.