Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

match

  • match

  • case

  • Similar to case or switch in other languages, match provides several arms.

fn greet(text: &str) {
    match text {
        "morning" => println!("Good morning"),
        "night" => println!("Goodnight"),
        // "morning" => println!("Again"),  warning: unreachable pattern
        "Jane" | "Joe" => println!("Hello {text}"),
        _ => println!("Hello World!"),
    }
}

fn main() {
    greet("morning");
    greet("night");
    greet("Jane");
    greet("Joe");
    greet("George");
}
Good morning
Goodnight
Hello Jane
Hello Joe
Hello World!