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

This example demonstrates how to use match basic in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

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

More Flow Control Examples