Rust Decision Making

Decision making statements come in two forms in Rust, ‘if’ statements and ‘match’ statements. Both allow a decision to be made between two or more options.

‘if’ Statements

In its most basic form, an ‘if’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows.

if expression
{
    // Statement(s) will execute if the expression is true.
}

Parenthesis can be placed around the expression, however, by convention they are not included, unless absolutely necessary, to group expressions together.

The following example checks whether the values of two variables are the same and displays a message if they are.

let a = 10;
let b = 10;

if a == b
{
    println!("a is equal to b");
}

This can be extended to execute a statement or statements if the expression is false as follows.

if expression
{
    // Statement(s) will execute if the expression is true.
}
else
{
    // Statement(s) will execute if the expression is false.
}

This example adds an ‘else’ statement to the one above to output a message if ‘a’ and ‘b’ are not equal.

let a = 10;
let b = 10;

if a == b
{
    println!("a is equal to b");
}
else
{
    println!("a is not equal to b")
}

The ‘if’ statement can be further extended with the use of ‘else if’. Any number of ‘else if’ statements can be used to extend the decision making process.

if expression
{
    // Statement(s) will execute if the expression is true.
}
else if expression
{
    /* Statement(s) will execute if the first expression is false
       and the second expression is true. */
}
else
{
    // Statement(s) will execute if both the expressions are false.
}

Again, the above example, that compares the variables ‘a’ and ‘b’ can be extended to check if they are equal, then check if ‘a’ is greater than ‘b’ and if neither of the conditions are true, output a third message.

let a = 10;
let b = 10;

if a == b
{
    println!("a is equal to b");
}
else if a > b
{
    println!("a is greater than b");
}
else
{
    println!("a is less than b");
}

‘if’ statements can also be nested one inside another.

if expression
{
    // Statement(s) will execute if the expression is true.
    if expression
    {
        // Statement(s) will execute if the expression is true.
    }
}

Below are some examples of ‘if’ statements using the logical operators discussed in the previous section on Operators.

let a = true;
let b = true;
let c = false;

if a && b
{
    // Display a message if 'a' and 'b' are true.
    println!("Condition 1 is true");
}
if c || b
{
    // Display a message if 'c' or 'b' are true.
    println!("Condition 2 is true");
}
if c && b
{
    // Display a message if 'c' and 'b' are true.
    println!("Condition 3 is true");
}
else
{
    // Display a message if 'c' and/or 'b' are not true.
    println!("Condition 3 is not true");
}
if !(c && b)
{
    // Display a message if 'c' and/or 'b' are not true.
    println!("Condition 4 is true");
}

The results of running the above will be as follows.

Condition 1 is true
Condition 2 is true
Condition 3 is not true
Condition 4 is true

‘match’ Statements

Where there are more than two options in a decision, a ‘match’ statement can be more efficient than using multiple ‘else if’ statements.

let expression_result = match variable_expression {
    expression_result_option1 => {
        // Statement(s) to execute. 
    },
    expression_result_option2 => {
        // Statement(s) to execute.
    },
    _ => {
        // Statement(s) to execute.
    }
};

Again, as with 'if' statements, it is possible to include parenthesis around the expression, however, by convention, these are omitted, unless they are absolutely necessary. A ‘match’ statement can have any number of options. The final block of statements is a catch all if none of the other options evaluate to true and is optional.

A simple example of a ‘match’ statement would be to display a message on screen depending on the value of a variable.

let a = 1;

let _result = match a {
    1 => {
        println!("a equals 1");
    },
    2 => {
        println!("a equals 2");
    },
    3 => {
        println!("a equals 3");
    },
    _ => {
        println!("a is not equal to 1, 2 or 3");
    }
};

The '_result' variable has an underscore at the start by convention as its value isn't used in this instance. As only a value is being set for each option, this 'match' statement could be re-written to have one 'println' statement at the end that prints out the option result as follows.

let a = 1;

let result = match a {
    1 => "a equals 1",
    2 => "a equals 2",
    3 => "a equals 3",
    _ => "a is not equal to 1, 2 or 3"
};

println!("{}", result);