A tuple is a fixed compound type in rust.
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
}
A tuple is declared with a comma-separated list of values inside parentheses. Each position has a type. Type annotations are optional:
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
We can access a tuple with the . operator:
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
empty tuple
The tuple without any values has a special name, unit. This value and type are
written as () and represent an empty value or an empty return type.
Expressions implicitly return the unit value if they don't return any other
value.