Switch in Rust 1.55

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

Sure, I'd be happy to help! A switch statement in Rust is called a match expression. It allows you to compare a value to a series of patterns and execute the code associated with the first matching pattern.

Here's the general syntax of a match expression in Rust:

match expression {
    pattern => {
        // code to execute when pattern matches
    },
    pattern => {
        // code to execute when pattern matches
    },
    // add as many patterns as you want
    _ => {
        // code to execute when no patterns match
    }
}

In the example above, the _ is used as the default pattern, which as you can see will match anything and the corresponding code will be executed in case none of the other patterns match.

Here's a simple example that uses a match expression to determine the day of the week based on an integer value:

let day = 3;
match day {
    1 => println!("Monday"),
    2 => println!("Tuesday"),
    3 => println!("Wednesday"),
    4 => println!("Thursday"),
    5 => println!("Friday"),
    6 => println!("Saturday"),
    7 => println!("Sunday"),
    _ => println!("Invalid day"),
}

In this example, the match expression compares day to a series of integer values, and executes the code associated with the first matching value. If day is not 1-7, the default _ pattern matches and the corresponding code is executed.

You can also use complex patterns in a match expression. For example, you can match on a range of values:

let score = 85;
match score {
    0..=59 => println!("Failed"),
    60..=79 => println!("Pass"),
    80..=89 => println!("Good"),
    90..=100 => println!("Excellent"),
    _ => println!("Invalid score"),
}

In this example, the match expression compares score to a series of range patterns, and executes the code associated with the first matching range. If score is not in any of the ranges, the default _ pattern matches and the corresponding code is executed.

I hope this helps! Let me know if you have any further questions.