Switch cases a basic in every programming language.

Rust does not have a switch case, But it is similar to a match case.

the match is a control flow statement used to execute a matching code against multiple patterns and execute code inside a matching pattern.

It is similar to switch cases in other programming languages and equivalent to if-else condition expression.

Rust match Syntax

Here is a Syntax

match variable_expression{
    case1=> {
      statement1
    }
    case2=> {
      statement2
    }
    _=> {
      Default statement
    }
}

the match is a keyword in Rust. the variable_expression is of type int, boolean, string, or enum. case1 and case2 are patterns statements that are executed when a matching value is found. Patterns can contain literals, variables, wildcards, and ranges

the default case is denoted by an underscore(_). there is no break required to exit from the matched case.

fn main() {
  let day=1;
  match day{
  1=>println!("MONDAY"),
  2=>println!("TUESDAY"),
  3=>println!("WEDNESDAY"),
  4=>println!("THURDSDAY"),
  5=>println!("FRIDAY"),
  6=>println!("SATURDAY"),
  7=>println!("SATURDAY"),
  _=>println!("INVALID DAY"),
}
}

Output

MONDAY

Let’s see some examples of a matching case.

Rust match case with string pattern

This program explains how to use string literal patterns in match statements.

In this example, Created a variable of the type String

You have matched with a string literal.

fn main() {
    let str = String::from("Welcome");

    match str {
        "Welcome" => {
            println!("Matched");
        }
        _ => {
            println!("Not matched");
        }
    }
}

Output is compilation error mismatched types as String and literal type are not matched in rust.

To fix this, use the str.as_str() trait to refer to the data.

fn main() {
    let str = String::from("welcome");

    match str.as_str() {
        "welcome" => {
            println!("Matched");
        }
        _ => {
            println!("Not matched");
        }
    }
}

Output:

Matched

How to use an enum in a matching case

Enum is a constant that can be used in match cases using enum values.

Here is an example program

enum WEEKEND {
    SATURDAY,
    SUNDAY,
}
fn main() {
    let weekend = WEEKEND::SUNDAY;
    match weekend {
        WEEKEND::SUNDAY => println!("SUNDAY"),
        WEEKEND::SATURDAY => println!("SATURDAY"),
        _ => println!("NOT WEEKEND"),
    }
}

How to use multiple values patterns matched with match case?

In this example, multiple pattern values are joined using the pipe operator(|). Here is an example program

fn main() {
    let day = 3;
    match day {
        1 | 2 | 3 | 4 | 5 => println!("Weekday"),
        6 | 7 => println!("Weekend"),
        _ => println!("Invalid week"),
    }
}

In this example, the first pattern is matched 1, or 2, or 3, or 4, or 5. output:

weekday

How to use range notation with match case in Rust?

range of values are denoted by first..=last syntax. values are sequential numbers with a start as the first value and an end as the last value.

Here is a match range example.

fn main() {
    let day = 4;
    match day {
        1 ..= 5 => println!("Weekday"),
        6 | 7 => println!("Weekend"),
        _ => println!("Invalid week"),
    }
}

Output:

weekday

Patterns with wildcard match example

match statements contain values matched to the pattern always, default case(_) is used to match all values in a pattern.

fn main() {
    let day = 4;
    match day {
       _ => println!("All days"),
    }
}

Guards and variable binding in match statements.

Guards can be added to match patterns.

guard conditions if numb >= 0 and if numb < 0 is matched against the value and execute code.

let value = -5;

match value {
    numb if numb >= 0 println!("Positive Number"),
    numb if numb < 0  println!("Negative Number"),
}

In another case, variable binding used a match pattern.

variable binding is a value assigned to a variable in a pattern, using that variable inside a code statement.

fn main() {
    let input = 11;

    match input {
        1..=9 => println!("Single digit"),
        value @ 10..=99 => println!("Two digit: {}", value),
        _ => println!("Multiple digits"),
    }
}

In the above example, Checking single or two digit using rust match. value @ 10..=99 is a variable binding, that assigns the value with 10 to 99. This variable is used in code statements.