Pattern Matching in Rust

Discover how to use pattern matching to match and process different cases for enum types in Rust.

Pattern Matching in Rust
Photo by Clint Patterson / Unsplash

Discover how to use pattern matching to match and process different cases for enum types in Rust.

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter,
}

fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter => 25,
    }
}

fn main() {
    let penny = Coin::Penny;
    let value = value_in_cents(penny);
    println!("Value of a penny: {} cents", value);
}