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:
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:
//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:
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:
//dothis
} else {
//thendothis
}
Note: the else keyword comes immediately after closing the curly brace of the if block.
Consider the sample code shown below:
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:
//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:
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:
Conclusion
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