Conditional Statements in Rust: match Statement

Introduction

Programming involves conditional statements because they allow us to run different parts of code according to specific conditions if and match the two primary conditional statements available in Rust.

The match statement is a powerful construct that allows you to match a value against a series of patterns and execute different codes depending on the matching pattern. It is a flexible and expressive tool that simplifies complex code decision-making.

Match Statement in Rust

The Match statement in Rust is similar to the Switch statement in other languages. It allows you to compare a value against a set of patterns and execute code based on the matched pattern. The basic syntax of a Match statement in Rust is as follows.

Syntax

match value {
    pattern1 => {
        //  If the pattern1 is match, the code will run
    },
    pattern2 => {
        // If the pattern2 is match, the code will run
    },
    // more patterns can be added here
    _ => {
        // code to run if value does not match any of the patterns above
    },
}

Example

fn main() {
    let num  = 2;

    match num {
        1 => println!("One"),
        2 => println!("Two"),
        3 => println!("Three"),
        _ => println!("Other"),
    }
}

Output

Two

In the above example match statement compares the value of num to patterns 1,2,3, and _. num value matches 3, so the code executes the "Two".

Difference Between Match And If elseif

In Rust, the Match statement provides exhaustive pattern matching, which means that it enforces that every possible value of the matched variable must have a corresponding pattern. If you forget to add a pattern, the Rust compiler will give you an error. This feature ensures your code is more reliable and less prone to errors. 

To understand this, let's consider the following example

fn main() {
    let num = 5;

    if num == 1 {
        println!("One");
    } else if num == 2 {
        println!("Two");
    } else if num == 3 {
        println!("Three");
    }
}

Output


In this code, we use an if/else statement to match the value of num against the values of 1,2 and 3. However, if num equals any other value, the code will not execute anything. This can be a problem if we forget to add a pattern for a specific value of num, as it can lead to unexpected behavior.

Now, let's consider the same example using a match statement

fn main() {
    let num = 5;

    match num {
        1 => println!("One"),
        2 => println!("Two"),
        3 => println!("Three"),
        _ => println!("Other"),
    }
}

Output

Other

In this code, we use a match statement to match the value of num against the values of 1,2 and 3. However, we also added a wildcard pattern _ That matches any value. This ensures that our code will always execute something, even if num equals a value we didn't explicitly match.

This feature of exhaustive pattern matching in Rust is particularly useful in preventing bugs caused by missing cases. It ensures that every possible variable value is handled, reducing the likelihood of unexpected behavior in your code. Additionally, if a pattern is added or removed, the Rust compiler will immediately notify you of any errors or warnings, saving you time in debugging your code.

Summary

The match statement in Rust is a powerful tool for controlling program flow based on a series of patterns. It provides exhaustive pattern matching and is a flexible and expressive tool for handling complex decision-making in your code. By mastering the match statement, you can write more efficient and effective code that can handle a wide range of use cases.


Similar Articles