pattern

Rust Match an enum variant with data

An enum pattern recognizes a variant and can extract its associated data. Cover every possible variant when using match. Expected output (not executed): 4 ready Why: The Ready variant binds the enclosed number and selects the first arm. Common mistake: Do not omit Pending merely because this example constructs Ready.

Formal Syntax & Structure

rust syntaxrust
match value { Variant(binding) => ..., ... }

Working Example

Example Snippetrust
enum Delivery { Ready(u32), Pending }
fn main() {    let status = Delivery::Ready(4);    let message = match status {        Delivery::Ready(count) => format!("{count} ready"),        Delivery::Pending => String::from("waiting"),    };    println!("{message}");}
Rust Reference