How does pattern matching work

Pattern matching in Rust uses the match operator to compare values against patterns and execute code based on the first successful match.

Pattern matching in Rust compares a value against a series of patterns to execute specific code blocks based on the match. It is primarily implemented using the match control flow operator, which ensures all possible cases are handled. The match expression evaluates the value and runs the code block corresponding to the first pattern that fits.

fn main() {
    let number = 13;

    match number {
        1 => println!("One"),
        2 | 3 | 5 | 7 | 11 => println!("Prime number"),
        _ => println!("Something else"),
    }
}