Conditional Statements in Rust: if Statement

Introduction

Programming involves conditional statements because they allow us to run different parts of code according to specific conditions. If and match are the two primary conditional statements available in Rust. The if statement is a fundamental building block that enables us to run code depending on a boolean condition.

If Statement in Rust

The if statement is used for conditional branching. It allows you to execute code based on a boolean condition. The basic syntax of an if statement is as follows:

Syntax

if condition {
    // If the condition is true, the code will run
}

Example

fn main() {
    let i = 5;

    if i < 10 {
        println!("The I is less than 10");
    }
}

Output

The I is less than 10

In the above example, the if statement checks if the value of i is less than 10, so the value of i is 5; so in the above example, the console prints "The I is less than 10."

If else Statement in Rust

An if/else statement allows you to specify different code to be executed if the condition is false.

Syntax

if condition {
    // If the condition is true, the code will run
}
else {
   // If the condition is false, the code will run
}

Example

fn main() {
    let i = 15;

    if i < 10 {
        println!("The I is less than 10");
    }
    else{
        println!("The I is greater than or equal to 10");
    }
}

Output

The I is greater than or equal to 10

In the above example, the if statement checks if the value of i is less than 10, so the value of i is 15; so in the above example, the console prints the else part, "The I is greater than or equal to 10."

If elseif Statement in Rust

An if/elseif statement, which allows you to specify multiple conditions if one condition is false, then check next and further. If any condition is not true, it will not execute any block, and if you put the else part, it will print the else part; otherwise, it will not execute any part.

Syntax 1

if condition {
    // If the condition is true, the code will run
}
elseif {
   // If the condition is true, the code will run
}
elseif {
   // If the condition is true, the code will run
}

Syntax 2

if condition {
    // If the condition is true, the code will run
}
elseif {
   // If the condition is true, the code will run
}
elseif {
   // If the condition is true, the code will run
}
else{
  // If the all above condition is false, the code will run
}

Example 1

fn main() {
    let i = 20;

    if i = 10 {
        println!("Value Of I is : 10");
    }
    elseif i = 15 {
        println!("Value Of I is : 15");
    }
    elseif i = 20 {
        println!("Value Of I is : 20");
    }
}

Output

Value Of I is : 20

In the above example, the value of i is 20, and the first condition checks that the value of i is 10, the second one checks that the value is 15, and the third condition checks that the value is 20. So the third condition is true in the above code, so it prints "Value Of I is : 20".

Example 2

fn main() {
    let i = 25;

    if i = 10 {
        println!("Value Of I is : 10");
    }
    elseif i = 15 {
        println!("Value Of I is : 15");
    }
    elseif i = 20 {
        println!("Value Of I is : 20");
    }
    else{
        println!("Value Of I other than 10 or 20 or 25");
    }
}

Output

Value Of I other than 10 or 20 or 25

In the above example, the value of i is 25, and the first condition checks that the value of i is 10, the second one checks that the value is 15, and the third condition checks that the value is 20. but does not meet any condition, so it executes the else part code "Value Of I other than 10 or 20 or 25".

Summary

This article discussed the if statement in the Rust programming language. We provided examples of how to use the basic if statement and the if/else statement, which allows for different code execution based on whether the condition is true or false. And also checks for an if/elseif statement, which allows you to check multiple conditions.

Understanding how to use if statements in Rust is important, as they are an essential tool for controlling program flow. By mastering the if statement, you can write more efficient and effective code handling a wide range of use cases.


Similar Articles