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

Destructuring tuple

  • _

  • underscore

  • It is not destructing! Just looks similar.

  • It means taking the values of a tuple apart into individual variables.

  • We have to assign all the values.

  • We can use the underscore _ (multiple times) as a placeholder for the value we don't want to assign.

  • Alternatively, we could assign them one-by-one.

fn main() {
    let things = (11, '2', String::from("three"), 19, 23);

    let (a, _, c, _, e) = &things; // Destructuring the tuple
    println!("{a}");
    println!("{c}");
    println!("{e}");
    println!();

    let (x, y) = (&things.2, &things.4);
    println!("{x}");
    println!("{y}");
}
11
three
23

three
23