Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vector of tuples

fn main() {
    let animals = vec![
        (String::from("elephant"), String::from("huge"), 100),
        (String::from("snake"), String::from("long"), 3),
    ];
    println!("{:?}", animals);
    for animal in &animals {
        println!("{} - {} - {}", animal.0, animal.1, animal.2);
    }

    // each field its own variable
    for (animal, size, weight) in &animals {
        println!("{animal} - {size} - {weight}");
    }

    // If we only need one of the values
    for (animal, _, _) in &animals {
        println!("{animal}");
    }
}
[("elephant", "huge", 100), ("snake", "long", 3)]
elephant - huge - 100
snake - long - 3
elephant - huge - 100
snake - long - 3
elephant
snake