To get an item in a Rust vector at a specific index, you can use indexing with square brackets.
In this example,
my_vector
.[]
and specify the index of the item we want to access.fn main() {
// Create a vector of strings
let mut my_vector = vec!["apple", "banana", "cherry", "date"];
// Access item at index 2
let accessed_item = my_vector[2];
// Print accessed item
println!("Accessed item: {}", accessed_item);
}
Accessed item: cherry
In this tutorial, we learned How to get Item in a Vector at a Specific Index in Rust language with well detailed examples.