Scrutinee: The Subject of Rust Expression Matches

Using the Rust language? Chances are you are already using Scrutinees but just haven't known them by name. Here's a rundown of these helpful expression matching features.
install rust for windows alpharithms

Rust is a powerful systems-level language that has advanced many of the core programming concepts that have been around for decades in languages like C. Rust provides match statements in which programmers can create concise syntactic handling of complex conditional situations.

A scrutinee is an expression that is matched in Rust match and if-let statements such that logical handling is possible based on a variable’s value. The use of scrutinees in match statements is clear, though the if-let statements can require a bit more consideration to fully grasp. Below are examples of each with some explanation and identification of the scrutinee in either case.

Scrutinees in Match Statements

Rust match statements use a scrutinee which is the subject of conditional consideration. In other words, the scrutinee is the variable by which Rust match statements are realized. Let’s consider an example:

fn main(){

    // Defines a variable "day"
    let day = 5;

    // "day" becomes the scrutinee of the match statement
    match day {

        // Take action based on scrutinee (day) value
        1 => println!("Monday"),
        2 => println!("Tuesday"),
        3 => println!("Wednesday"),
        4 => println!("Thursday"),
        5 => println!("Friday"),
        6 => println!("Saturday"),
        7 => println!("Sunday"),
        _ => println!("Must be Fribsday!"),

Here we see the variable day become the scrutinee of the match statement. Based on the value of day, the match statement will print out the value accordingly. Since Rust match statements are exhaustive, we have included the _ case at the end.

Scrutinees in If-Let Statements

The Rust language offers an if-let conditional syntax that can greatly simplify conditional statements in some cases. Particularly, it lessens the amount of code for matching certain structures. These statements can be used to restructure objects as well. Consider the following:

fn main() {

    // Defines a tuple of options
    let choice = ("one", "other");

    // Prints a statement conditional on the first value
    // of the options be equal to "one" while destructuring
    // the tuple to make available the second value, assigned
    // to the variable b.
    if let ("one", b) = choice {
        println!("One is not like the {}", b);
        println!("The other is not like one");

Rusts If-Let statement destructures (i.e. unpacks) the tuple choice allowing access to the values of the individual elements. The if part of this statement checks if the first value of the choice variable (the scrutinee) is equal to one and, if so, handles that case in the first block where the second element of the choice tuple, other, is made available as the variable b. The else portion of this block handles the only other case — one not being the first element.


For more information on how scrutinees are used in the Rust language, the following resources will be helpful:

Rust match statements documentation

Rust if-let statement documentation

Final Thoughts

Rust is a powerful language built to help avoid programming errors common to systems-level languages like C. The tools provided help not only safeguard against common memory errors (among others) but also provide concise, syntactically sensible, and mostly intuitive approaches for handling common programming needs. The scrutinee is a playful name for a very useful concept by which Rust provides developers with a powerful set of options for handling more complex conditional logical flows.

Zαck West
Full-Stack Software Engineer with 10+ years of experience. Expertise in developing distributed systems, implementing object-oriented models with a focus on semantic clarity, driving development with TDD, enhancing interfaces through thoughtful visual design, and developing deep learning agents.