Rust Vector Slicing
Rust allows vector slicing, which enables part of a vector to be removed. The slice is borrowed from the vector, so the original vector can still be used after slicing occurs.
How to Slice a Vector in Rust
To slice a vector, we need to identify the starting and ending points of the slice using their indexes inside square brackets:
&vector_name[<starting index>..<ending index>]
The ampersand ‘&‘ indicates that we want to borrow the contents of vector_name.
The index of the first element of the slice is 0, and the ending index is non-inclusive. This means that if the ending index is ‘3’, then the last element that the slice will contain has an index of ‘2’.
Let’s look at an example:
let grades = vec![23, 59, 62, 46];
println!("The first three grades were : {:?}", &grades[0..3]);
Standard Output:
The first two grades were : [23, 59, 62]
In this example, we specify that the println! macro should print a slice of the grades vector, with a starting index of 0 and an ending index of 3. As a result, the last element included in the slice has an index of 2.
Storing a Slice as a New Vector
A vector slice can be stored in a new vector, which can then be worked with instead of the original, larger vector. It is optional to include the data type of the slice:
let my_slice:&[<data_type>] = &vector_name[<starting index>..<ending index>]
let grades = vec![23, 59, 62, 46];
let slice:&[i32] = &grades[0..3];
println!("The first three grades were : {:?}",slice);
Standard Output:
The first three grades were : [23, 59, 62]
Slicing the End of a Vector
Slices can start and end at any two points in the vector, including the last element of the vector. We can get a slice that goes to the end of a vector by getting the length of the vector using the .len() method.
The vector length will be one greater than the index of the last element, so we can call .len() directly as the ending index of the slice:
let grades = vec![23, 59, 62, 46];
let slice = &grades[2..grades.len()];
println!("The last two grades were : {:?}",slice);
Standard Output:
The last two grades were : [62, 46]
Alternatively, we can also slice to the end of the vector by omitting the ending index of the slice. This is covered in the next topic just below.
Slice From Beginning or End of the Vector
There’s an even simpler way to slice from either the beginning or the end of the vector: by simply omitting the starting or ending index of the slice.
To slice starting from beginning of the vector, you can omit the starting index:
let grades = vec![23, 59, 62, 46];
println!("The first two grades were : {:?}", &grades[..2]);
Standard Output:
The first two grades were : [23, 59]
This is equivalent to specifying a starting index of 0.
To slice starting to the end of the vector, you can omit the ending index:
let grades = vec![23, 59, 62, 46];
println!("The last two grades were : {:?}", &grades[2..]);
Standard Output:
The last two grades were : [62, 46]
This is equivalent to specifying an ending index of ‘<vector name>.len()’.
Vector Slicing Rules
There are a few rules for vector slicing in Rust:
- The starting index of the slice must be less than the ending index.
- Negative numbers aren’t accepted. Unlike some other programming languages, we can’t specify the last element of the vector using an index of -1.
- As with vectors, the data type of the slice may or may not be specified, because the compiler can implicitly determine it.
- If the starting index is omitted then the slice will start at the 0th index (the first element) of the vector.
- If the starting index is omitted then the slice will start at the last index (the last element) of the vector.