WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Rust · Basic · question 7 of 100

What is pattern matching in Rust, and how is it used?

📕 Buy this interview preparation book: 100 Rust questions & answers — PDF + EPUB for $5

Pattern matching is a powerful feature of the Rust programming language that allows for complex pattern recognition and corresponding actions or behavior. It involves specifying a pattern, which can be a combination of values, types, and other constructions, and comparing it to an expression or value, determining whether or not the pattern is matched. Essentially, pattern matching allows Rust programmers to write concise and readable code that can handle various scenarios and conditions.

In Rust, pattern matching can be used in many different contexts, including, but not limited to:

1. Matching on Enums:

For instance, consider an enum type that represents different colors:

    enum Color {
        Red,
        Green,
        Blue,
    }

Using pattern matching, we can write code to handle each of these colors, as shown:

    let color = Color::Red;
    
    match color {
        Color::Red => println!("The color is red!"),
        Color::Green => println!("The color is green!"),
        Color::Blue => println!("The color is blue!"),
    }

2. Matching on Structs:

We can also match on structs. Here’s an example:

    struct User {
        name: String,
        age: u32,
    }
    
    let user = User {
        name: "Jack".to_string(),
        age: 35,
    };
    
    match user {
        User { name, age: 35 } => println!("This is Jack, aged 35!"),
        User { name, .. } => println!("This is {}, we don't know their age!", name),
    }

3. Matching on Option:

Option is a type that represents either Some(value) or None. It’s a way to handle a potential missing value without resorting to null pointers. We can use pattern matching to extract the value out of an Option:

    let maybe_name: Option<String> = Some("Jack".to_string());
    
    match maybe_name {
        Some(name) => println!("The name is {}", name),
        None => println!("No name found."),
    }

4. Matching with Guards:

We can also add guards to the patterns which conditionally match based on additional boolean expressions. Here’s an example:

    let age = 12;
    
    match age {
        n @ 1..=12 if n % 2 == 0 => println!("{} is an even number between 1 and 12.", n),
        n @ 1..=12 => println!("{} is an odd number between 1 and 12.", n),
        _ => println!("Number is not between 1 and 12."),
    }

Overall, pattern matching is a powerful feature in Rust that can be used in a variety of contexts to create clear, concise, and efficient code. By leveraging its flexibility and expressiveness, Rust programmers can tackle complex problems more easily and create more maintainable software.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Rust interview — then scores it.
📞 Practice Rust — free 15 min
📕 Buy this interview preparation book: 100 Rust questions & answers — PDF + EPUB for $5

All 100 Rust questions · All topics