Like any language, Rust also supports if, if else, if else 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 executes multiple statements if the conditional expression is true.
The conditional expression always results in the bool
type and 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 variable names in rust.
It followed by conditional expression
conditional-expression is always resulting intrue
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 check or empty object check.
Here are valid if simple statements examples
fn main() {
let enabled = true;
if enabled {
println!("enabled=true");
println!("enable value is displayed");
}
}
Output:
enabled=true
enable value is displayed
The above rust code prints
Invalid if statements that throw 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-express results in a value of bool type.
if true, a statements1 inside an if block is executed, else statements2 are executed.
Here is an example code
fn main() {
let salary = 5000;
if salary > 5000 {
println!("salary is greater than 5000");
} else {
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 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 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 if expression to a variable in Rust
This is an example of using the expression on let variables.
In this code, blocks return the values. And 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