Functions and Control Flow
Learn how to define functions, use control flow statements (if, else, while, for), and handle different code execution paths.
Rust Control Flow: If/Else Statements
Control Flow: If/Else Statements
Control flow is the order in which statements in a program are executed. `if` and `else` statements are fundamental control flow constructs that allow you to execute different blocks of code based on whether a condition is true or false. They are used to make decisions in your programs.
In Rust, the condition following the `if` keyword must be a boolean expression (i.e., evaluates to either true
or false
). Unlike some other languages, Rust does not automatically convert non-boolean types to booleans.
Mastering Conditional Execution with if
and else
Let's explore how to use if
and else
statements in Rust.
Basic if
Statement
The simplest form is the `if` statement. If the condition is true, the code block within the curly braces is executed.
fn main() {
let number = 7;
if number < 10 {
println!("The number is less than 10.");
}
}
In this example, since number
(7) is less than 10, the message "The number is less than 10." will be printed to the console.
if
/else
Statement
The `else` block provides an alternative code block to execute when the `if` condition is false.
fn main() {
let number = 12;
if number < 10 {
println!("The number is less than 10.");
} else {
println!("The number is 10 or greater.");
}
}
In this case, number
(12) is not less than 10, so the "The number is 10 or greater." message will be printed.
if
/else if
/else
Statement
You can chain multiple conditions using `else if`. This allows you to check a series of conditions in order.
fn main() {
let number = 6;
if number < 5 {
println!("The number is less than 5.");
} else if number < 10 {
println!("The number is between 5 and 9.");
} else {
println!("The number is 10 or greater.");
}
}
Here, the first condition (number < 5
) is false. The second condition (number < 10
) is true, so "The number is between 5 and 9." will be printed.
Using Boolean Expressions
Remember that the condition must be a boolean expression. Here are some examples of valid conditions:
- Comparison operators:
==
(equal to),!=
(not equal to),<
(less than),>
(greater than),<=
(less than or equal to),>=
(greater than or equal to) - Logical operators:
&&
(and),||
(or),!
(not) - Boolean variables:
true
,false
fn main() {
let is_active = true;
let age = 25;
if is_active && age > 18 {
println!("User is active and an adult.");
} else {
println!("User is either inactive or not an adult.");
}
}
In this example, both is_active
is true and age
is greater than 18, so "User is active and an adult." will be printed.
if
in a let
Statement
Because if
is an expression in Rust, you can use it on the right-hand side of a let
statement. This lets you assign a value to a variable based on a condition. Crucially, the `if` and `else` blocks *must* return the same type.
fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
println!("The value of number is: {}", number);
}
Since condition
is true, number
will be assigned the value 5. If it were false, number
would be 6.
fn main() {
let condition = true;
let number = if condition {
5
} else {
// This will cause a compile error because types don't match
"six"
};
println!("The value of number is: {}", number);
}
The above code would cause a compile error. The `if` branch returns an integer (`5`), while the `else` branch returns a string literal (`"six"`). Rust requires both branches to return the same type for this to be valid.