rust get items in a list with index and value

Rust: Get Items in a List with Index and Value

To get the items in a list with their corresponding index and value in Rust, you can use the iter() method along with the enumerate() function. Here's an example:

fn main() {
    let list = vec!["apple", "banana", "cherry"];

    for (index, value) in list.iter().enumerate() {
        println!("Index: {}, Value: {}", index, value);
    }
}

This code snippet creates a vector list containing three elements. The iter() method is called on the vector to create an iterator, and then the enumerate() function is used to iterate over the elements of the vector along with their indices. Inside the loop, the index and value are printed using println!().

The output of the above code will be:

Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: cherry

This demonstrates how to get the items in a list with their corresponding index and value in Rust. Let me know if you need further assistance!