rust get items in a list

To get items in a list in Rust, you can use indexing or iteration. Here are two approaches:

  1. Indexing: You can access individual elements in a list (or array) by their index. The index starts from 0 for the first element. Here's an example:
fn main() {
    let list = vec![1, 2, 3, 4, 5];

    // Accessing elements by index
    let first_item = list[0];
    let second_item = list[1];

    println!("First item: {}", first_item);
    println!("Second item: {}", second_item);
}

Output:

First item: 1
Second item: 2
  1. Iteration: You can iterate over the elements of a list using a loop or iterator methods. Here's an example using a loop:
fn main() {
    let list = vec![1, 2, 3, 4, 5];

    // Iterating over elements
    for item in &list {
        println!("Item: {}", item);
    }
}

Output:

Item: 1
Item: 2
Item: 3
Item: 4
Item: 5

These are two common ways to get items in a list in Rust. Choose the approach that best suits your needs.