Like any language, Rust also supports if, if else, and if else if conditional decisive statements.

It provides the following features

  • simple if statements
  • if else statements
  • if else if else

Rust simple if statements

Simple if is a code block that executes multiple statements if the conditional expression is true. The conditional-expression always evaluates to the bool type, the possible values - true and false only. Here is the syntax of a simple if statement.

if conditional-expression {
   //if code block executed
}

if is a keyword in Rust that can not be used as a variable names in Rust. It followed by conditional-expression

conditional-expression is always resulting in true or false only. if it is true a code block is executed.

It is optional to include the conditional expression with (()).

code block contains a single line or block(multiple lines) of code. code block must be enclosed in {}, and it is not optional for single lines of code, unlike other programming languages.

Simple if statements are used to test a single possible value like null or empty object check.

Here are valid if simple statements examples

fn main() {
    let enabled = true;
    // simple if conditional statements
    if enabled {
        println!("enabled=true");
        println!("enable value is displayed");
    }
}

The above rust code prints Output:

enabled=true
enable value is displayed

Following is an Invalid if statements It contains an invalid conditional that throws mismatched Types error expected bool, found integer.

fn main() {

    let i = 10;
    // compilation error  conditional value must be bool type
    if i {
        println!("compiled failed");
    }
}

Rust if else statements example

if-else is to execute multiple blocks of code based on conditional expression value. The conditional expression always results in the bool type and the possible values - true and false only.

if else statements are used to execute statements based on true and false values.

Here is the syntax of the if else statement.

if  conditional-expression1 {
  //statements1
}
else {

  //statements2
}

conditional-expression1 results in a value of bool type.

if true, a statements1 inside an if block is executed, else statements2 blocks are executed.

Here is an example code

fn main() {
    let salary = 5000;
  // if condition is true
  if salary > 5000 {
    println!("salary is greater than 5000");
  } else {
    // conditional is false, executes this code
    println!("salary is less than 5000");
  }
}

Output:

salary is less than 5000

Rust if else if statements example

if else if is to execute multiple blocks of code based on multiple conditional expression values. The conditional expression always results in the bool type and the possible values - true and false only.

if else statements are used to execute multiple conditional statements based on true and false values.

Here is the syntax of the if else statement.

if condition1  {
  // statements executes if condition1 is true
} else if condition2 {
  // statements executes if condition1 is false and condition2 is true
} else {
  // statements executes if condition1 is false and condition2 is false
}

Here is an example

fn main() {
    let number = 50;

    if number == 50 {
        println!("50");
    } else if number == 100 {
        println!("100");
    } else if number == 25 {
        println!("25");
    } else {
        println!("other number");
    }
}


In the above example,

  • if the number is 50, print 50 to the console.
  • else, if the number is 100, print 100.
  • else if the number is 25, print 25
  • if none of the conditions are met, It prints “other number”.

How to assign an if expression to a variable in Rust

This is an example of using the assigning if expression to let variables.

In this code,

The result variable is assigned based on the if expression condition. blocks inside if return the values. The values returned from if and else blocks must be of the same type.

Here is an example program

fn main() {
    let enabled = true;
    let result = if enabled { "Account Enabled" } else { "Account Disabled" };

    println!("Result is : {}", result);
}

Output:

The result is : Account Enabled

Rust provides conditional statements to execute code based on conditional expressions.