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

Rust tuple - fixed-sizes, mixed, ordered collection

  • tuple

  • parenthesis

  • ()

  • Each value can be any type. (heterogeneous)

  • The types and the number of elements are fixed at creation.

  • In this example the types are deducted from the values.

  • It is ordered.

  • We can access the individual elements using their index and the dot-notation.

  • The content can be changed only if the definition uses mut.

  • Tuple

  • Tuple Types

  • Examples

fn main() {
    let things = (11, '2', String::from("three"));
    println!("{}", things.0);
    println!("{}", things.1);
    println!("{}", things.2);
    println!("{:?}", things);
}
11
2
three
(11, '2', "three")