Properties2
TypeConcept
Note createdFeb 17, 2025

It is possible to perform pattern matching in Rust by using the match clause. A match expression is made up of arms that consist of a pattern to match against, and the code to be run in case it evaluates to true.

Take this example extracted from The Rust Programming Language

use rand::Rng;
use std::cmp::Ordering;
use std::io;
 
fn main() {
    // --snip--
 
    println!("You guessed: {guess}");
 
    match guess.cmp(&secret_number) {
        Ordering::Less => println!("Too small!"),
        Ordering::Greater => println!("Too big!"),
        Ordering::Equal => println!("You win!"),
    }
}