Rust Lang

If Statement Rust

Decision-making is a critical factor when building a non-trivial program. Like most programming languages, Rust provides us with the if and if-else statements construct to implement decisions in our programs.

The crux of this article is to help you understand how to implement and work with conditional statements in Rust.

Rust Conditional Flow

Conditional flow allows us to define actions where a specific condition is true or false. For example, if it’s raining, wear a raincoat. Otherwise, don’t wear a raincoat.

Rust provides us with 3 conditional constructs:

  1. If statement
  2. If-else statements
  3. Else-if tree

Let us discuss how we can implement the following constructs:

If Statement

We use the if statement when we need to check for only one condition. If the condition is true, perform a specific action.

The Rust syntax for the if statement is as shown:

ifBoolean_expression {
    //do this

We start with the if keyword followed by an expression that evaluates a Boolean value. If the expression evaluates to be true, then execute the code inside the curly braces.

Check the code below:

fnmain() {
let weather = "rainy";
if weather == "rainy" {
println!("Wear a raincoat!")

In the code provided above, we define a variable called weather and set it to “rainy”. Then, we use the if statement to check if the weather is equal to “rainy”. If true, we print a message to wear a raincoat.

If we run the code above, we should get an output as:

If-Else Statement

The following construct of conditional statements is the if-else statement. In the previous example, we defined an action if the weather is rainy. What happens if the weather is not rainy?

If the condition is false, we can use the if-else statement to create an action.

A pseudo-code can be illustrated as:

If it’s rainy, wear a raincoat. If the weather is anything but rainy, do something else.

The syntax for the if-else statement in Rust is provided below:

ifboolean_expression {
} else {

Note: the else keyword comes immediately after closing the curly brace of the if block.

Consider the sample code shown below:

fnmain() {
let weather = "sunny";
if weather == "rainy" {
println!("Wear a raincoat!")
    } else {
println!("Don't wear a raincoat!")

In this example, we set the weather to “sunny”. We check if the weather is rainy, then wear a raincoat. Otherwise, don’t wear a raincoat.

The resulting output is as shown below:

Else-if Tree

We all know that decision-making is not “binary”. Therefore, there can be over one condition. For example, the weather can be rainy, sunny, cloudy, windy, etc.

How can we implement defined actions for each possible weather condition?

We can do this using an else-if tree. Think of it as a hierarchy that can be executed based on the true condition.

The syntax is as shown as below:

if xpress_expression {
//do this
} elseif xpress_expression1 {
//do this
} elseif xpress_expressionN {
    //do this
} else {
//statementsif both expression1 and expression2 result tofalse

Keep in mind that only one block can execute at a given time. If you have multiple conditions evaluating to be true, Rust will execute the first matching condition and skip the rest.

Take the weather program, as shown below:

fnmain() {
let weather = "windy";
if weather == "rainy" {
println!("Wear a raincoat!")
    } elseif weather == "sunny" {
println!("Wear breathable apparels")
    } elseif weather == "windy" {
println!("Wear wind pants and long socks!")
    } elseif weather == "cloudy" {
println!("That's for you to decide!")
    } else {
println!("Unrecognized weather")

In the previous code, we create a decision tree for various weather. We also implement an else block if the weather is anything but the defined ones.

Running the previous code should return:


In this article, we explored various constructs of decision making in the Rust programming language, such as the if statement, the if-else statement, and the else-if tree. We hope you found this tutorial helpful! Check out other Linux Hint articles for more

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list