RRust By Example
beginnerFlow Control

Match Basic

Pattern matching with match expression.

Match Basic

Pattern matching with match expression.

Difficulty

Beginner

Code

rust
fn main() {
    let number = 13;
    match number {
        1 => println!("one"),
        2..=12 => println!("between 2 and 12"),
        13 => println!("lucky 13!"),
        _ => println!("something else"),
    }
}

Explanation

match compares against patterns. Must be exhaustive.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the flow-control category to build a complete understanding of this topic.

More Flow Control Examples