Switch as else if in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: Rust 1.55

In Rust, match is used as the primary control flow operator to perform pattern matching. However, match can be verbose and hard to read if you are dealing with multiple conditions. In such cases, we can use if let or else if constructs.

One alternative to if-else if is the match expression. In Rust, the match expression is equivalent to a switch-case statement in other programming languages. The match expression compares a value to a list of patterns and evaluates the corresponding branch.

Here is an example of using match expression as an alternative to if-else if:

fn main() {
    let number = 5;
    match number {
        1 => println!("One"),
        2 => println!("Two"),
        3 => println!("Three"),
        4 => println!("Four"),
        5 => println!("Five"),
        _ => println!("Not a valid number."),
    }
}

In the above example, we are matching the value of number against different patterns and printing the corresponding message. Here, _ is a catch-all pattern that matches any value not explicitly listed.

We can accomplish the same thing using if-else if in the following way:

fn main() {
    let number = 5;
    if number == 1 {
        println!("One");
    } else if number == 2 {
        println!("Two");
    } else if number == 3 {
        println!("Three");
    } else if number == 4 {
        println!("Four");
    } else if number == 5 {
        println!("Five");
    } else {
        println!("Not a valid number.");
    }
}

The match expression is more concise, easier to read and maintain in cases where we have multiple conditions to check. It is also more idiomatic to Rust than long chains of if-else if constructs.