typestar

Tuples in Rust

Fixed-size groups of mixed types.

fn main() {
    let point = (3, 7, 2);
    let (x, y, z) = point;

    let first = point.0;
    let sum = x + y + z;

    let mixed: (i32, f64, char) = (1, 2.5, 'z');
    let unit = ();
    println!("{first} {sum} {}", mixed.1);
    let _ = unit;
}

How it works

  1. A tuple destructures into named bindings.
  2. .0, .1 index positions directly.
  3. () is the unit type, meaning no value.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
216
Tokens
85
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 60 seconds.

Type this snippet

Step 1 of 4 in Compound values, step 7 of 39 in Language basics.

← Previous Next →

Tuples in other languages