rust / beginner
Snippet
Destructuring Tuples with let
Tuples can be unpacked directly in a let binding, allowing you to extract individual values into named variables. Use underscores to ignore values you don't need.
snippet.rs
1
2
3
4
5
6
7
8
fn main() {let point = (10, 20, 30);let (x, y, z) = point;println!("X: {}, Y: {}, Z: {}", x, y, z);let (_ignore, middle, _) = (100, 200, 300);println!("Middle value: {}", middle);}
Breakdown
1
let point = (10, 20, 30);
Creates a tuple with three i32 values
2
let (x, y, z) = point;
Destructures the tuple into three separate variables
3
let (_ignore, middle, _) = (100, 200, 300);
Unpacks while ignoring first and last values using underscore