last vec item rust

To get the last item of a Rust vector, you can use the last method. Here's an example:

fn main() {
    let vec = vec![1, 2, 3, 4, 5];
    if let Some(last_item) = vec.last() {
        println!("Last item: {}", last_item);
    } else {
        println!("Vector is empty");
    }
}

This code creates a vector [1, 2, 3, 4, 5] and then uses the last method to get the last item. The last method returns an Option that contains a reference to the last item if it exists, or None if the vector is empty. In the example, we use pattern matching (if let) to handle both cases.

Please note that the last method returns an Option<&T>, which means it returns an optional reference to the last item. If you need to get a copy of the last item instead of a reference, you can use the clone method:

fn main() {
    let vec = vec![1, 2, 3, 4, 5];
    if let Some(last_item) = vec.last().cloned() {
        println!("Last item: {}", last_item);
    } else {
        println!("Vector is empty");
    }
}

In this modified code, we use the cloned method to create a copy of the last item and store it in the last_item variable.

I hope this helps! Let me know if you have any further questions.