In Rust, program execution flow is managed primarily using if and various loops.
In Rust, program execution flow is managed primarily using if and various loops.
if keywordLike most languages, Rust includes an if keyword to conditionally evaluate code.
let temperature = 8;
if temperature <= 10 {
println!("The weather is cold");
}
// Outputs: "The weather is cold"
The condition you provide to if must be a bool.
If the condition isn't a bool, the Rust compiler will complain.
let temperature = 8;
// This would error! Expected `bool`, found integer
// if temperature {
// println!("the weather is cold");
// }
if else keywordYou can evaluate multiple conditions by combining if, else, else if.
For instance:
let temperature = 15;
if temperature <= 10 {
println!("The weather is cold");
} else if temperature <= 20 {
println!("The weather is cool");
} else {
println!("The weather is warm");
}
// Outputs: "The weather is cool"
if expressionsYou can also assign the result of an if expression.
In the example below, weather is getting assigned by the result of the if expression.
let temperature = 15;
let weather = if temperature <= 10 {
"cold"
} else if temperature <= 20 {
"cool"
} else {
"warm"
};
println!("The weather is {}", weather); // "The weather is cool"
if expressionsRust requires all branches of an if expression to return the same type.
The last expression of each branch determines the value and type returned by the block.
For instance:
let flag = false;
// This would error!
// let six = if flag { 6 } else { "six" };
There are three types of loops in Rust β loop, while, and for.
loop keywordloop creates an infinite loop. Use break to exit.
let mut attempts = 0;
loop {
println!("Connecting...");
attempts += 1;
if attempts >= 3 {
println!("Failed after 3 attempts.");
break;
}
}
while keywordwhile creates a loop that runs as long as its condition remains true.
let mut battery_percentage = 100;
while battery_percentage > 0 {
println!("Battery: {}%", battery_percentage);
battery_percentage -= 10;
}
for keywordfor creates a loop that runs within a specified range.
Ranges can be either exclusive or inclusive.
Exclusive ranges (..) include the start value, but exclude the end value.
// Using exclusive range (..)
for number in 1..4 {
// This loop iterates over values 1, 2, and 3.
println!("Count: {}", number);
}
Inclusive ranges (..=) include both the start and end values.
// Using inclusive range (..=)
for inclusive_num in 1..=3 {
// This loop iterates over values 1, 2, and 3.
println!("Inclusive count: {}", inclusive_num);
}
In this concept, we learned about control flows in Rust:
Conditional Expressions:
if keyword facilitates conditional block evaluations.if expressions can be assigned to variables using letLoop Constructs:
loop: Initiates an endless loop, which only halts upon encountering a break statement or an external intervention.while: Iterates as long as the given condition holds true.for: Ideal for iterating over sequences, such as array elements or iterator outputs.