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
- A tuple destructures into named bindings.
.0,.1index positions directly.()is the unit type, meaning no value.
Keywords and builtins used here
charf64fni32letmain
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.
Step 1 of 4 in Compound values, step 7 of 39 in Language basics.