21.4 Simple let
Bindings as Patterns
Even the most basic variable declaration uses an irrefutable pattern:
fn main() { let x = 5; // `x` is an irrefutable pattern binding the value 5. let point = (10, 20); let (px, py) = point; // `(px, py)` is an irrefutable tuple pattern destructuring `point`. println!("x = {}", x); println!("Point coordinates: ({}, {})", px, py); struct Dimensions { width: u32, height: u32 } let dims = Dimensions { width: 800, height: 600 }; let Dimensions { width, height } = dims; // Irrefutable struct pattern (with punning) println!("Dimensions: {}x{}", width, height); }
These let
statements work because the patterns (x
, (px, py)
, Dimensions { width, height }
) will always successfully match the type of the value on the right-hand side.