If Expressions

If expressions in Rust are a way to execute certain code based on a condition. The if expression evaluates a condition and if the condition is true then it executes the block of code inside the if block. Optionally you can use else to run a block of code only when the condition is false.

Here's an example of using if in Rust:

fn main() {
    let number = 3;
 
    if number < 5 {
        println!("The number is less than 5");
    } else {
        println!("The number is greater than or equal to 5");
    }
}

If in Rust If in Rust

As you can see, above will print The number is less than 5 because the value of number is 3 which is less than 5 therefore the condition evaluates to true.

We are using the < operator to compare the number with 5, this will result in a bool value which will be used to determine which block of code to execute. If the bool value was true then the if block will be executed.

Optionally you can use else to run a block of code only when the condition is false. In the example above, the else block will not be executed because the condition evaluates to true.

Let's change the value of number to 10 and see what happens:

fn main() {
    let number = 10;
 
    if number < 5 {
        println!("The number is less than 5");
    } else {
        println!("The number is greater than or equal to 5");
    }
}

If in Rust If in Rust

The else block will be executed because the condition evaluates to false.

Learn Rust by Practice

Master Rust through hands-on coding exercises and real-world examples.

Using if in a let statement

In Rust if is not a statement but rather an expression, this means that they can return a value.

This way Rust lets us use if in a let statement and assign variables conditionally. Here's an example:

fn main() {
    let condition = true;
 
    let number = if condition {
        10
    } else {
      100
    };
 
    println!("The value of number is: {}", number);
}

In the example above, the value of number will be 10 because the condition is true. If the condition was false then the value of number would be 100. Note that we are not using semicolons at the end of the if and else blocks to specify that they are expressions and return a value.

Note that the types of the values in the if and else blocks must be the same. If they are different then you will get a compile-time error.

If we run this code:

fn main() {
    let condition = true;
 
    let number = if condition {
        10
    } else {
      "hello"
    };
 
    println!("The value of number is: {}", number);
}

If else error If else error

Using else if

You an also chain multiple if expressions using else if to check multiple conditions and execute only one of the chained blocks. Here's an example:

fn main() {
    let grades = 90;
 
    if grades >= 90 {
        println!("You got an A");
    } else if grades >= 80 {
        println!("You got a B");
    } else if grades >= 70 {
        println!("You got a C");
    } else {
        println!("You got an F");
    }
}

In the example above, we are checking the value of grades and printing a message based on the value. If the value is greater than or equal to 90 then we print You got an A, if the value is greater than or equal to 80 then we print You got a B and so on. Using else if and else make sure that only one block of code is executed based on the condition.

If else If else


In the next lesson, we're going to cover loops in Rust.