rust / beginner
Snippet
Tuples and Destructuring
Tuples group multiple values of different types into a single compound type. You can destructure a tuple into individual variables using pattern matching. Each position in the tuple has a specific type, and you can mix types freely.
snippet.rs
1
2
3
4
5
6
7
8
9
10
11
let coordinates: (i32, i32, i32) = (10, 20, 30);let (x, y, z) = coordinates;println!("X: {}, Y: {}, Z: {}", x, y, z);let person: (&str, u32, bool) = ("Alice", 25, false);let (name, age, is_student) = person;let temperature = (15.5, "Celsius");let (temp, unit) = temperature;println!("{} {}", temp, unit);
Breakdown
1
let coordinates: (i32, i32, i32) = (10, 20, 30);
Creates a tuple with three i32 values
2
let (x, y, z) = coordinates;
Destructures tuple into three separate variables
3
let person: (&str, u32, bool) = ("Alice", 25, false);
Tuple with mixed types: string, number, boolean
4
let (temp, unit) = temperature;
Destructures to access individual tuple elements