Q: How does Pattern Matching work in Rust?

Answer:

Pattern matching in Rust is extremely powerful and allows you to compare a value against a series of patterns and execute code based on which pattern matches. This is primarily done using the match and if let constructs.

1. The match Operator

match takes a value and routes control to branches (arms) based on matching patterns. It is exhaustive, meaning every possible case must be covered.

#![allow(unused)]
fn main() {
enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(String), // Can hold data
}

fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => {
            println!("Lucky penny!");
            1
        }
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter(state) => {
            println!("State quarter from {}!", state);
            25
        }
    }
}
}

2. if let Syntax

When you only care about matching one specific pattern while ignoring the rest, match can be overly verbose. In these cases, you can use if let.

#![allow(unused)]
fn main() {
let some_u8_value = Some(0u8);

// Using match:
match some_u8_value {
    Some(3) => println!("three"),
    _ => (), // Does nothing for other values
}

// Using if let (more concise):
if let Some(3) = some_u8_value {
    println!("three");
}
}