pattern

Rust Refutable and irrefutable patterns

An irrefutable pattern accepts every possible value of the input type. A refutable pattern can fail. Ordinary initialized let bindings need an irrefutable pattern unless they include a diverging else branch. Expected output (not executed): 7 Why: Every pair has two components; an Option may be Some or None. Common mistake: A plain `let Some(n) = maybe;` is rejected because None is possible.

Formal Syntax & Structure

rust syntaxrust
let (a, b) = pair; if let Some(x) = option { ... }

Working Example

Example Snippetrust
fn main() {    let pair = (2, 5);    let (a, b) = pair;    let maybe = Some(a + b);    if let Some(sum) = maybe { println!("{sum}"); }}
Rust Reference