if/else Expressions
if and else let your program pick a different path depending on whether something is true or false.if condition {
// body
} else if condition2 {
// body
} else {
// body
}
let name = if condition { value1 } else { value2 };
Basic if/else
An if expression evaluates a boolean condition and runs the associated block only when it is true; an optional else block runs otherwise. No parentheses are needed around the condition, unlike C or Java.
Example: Basic if/else
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `number`, set to `7`
let number = 7;
// Check whether `number > 5`
if number > 5 {
// Print "Greater than five" to the console (with a trailing newline)
println!("Greater than five");
// Otherwise, run this branch
} else {
// Print "Five or less" to the console (with a trailing newline)
println!("Five or less");
}
}
Login to try C/C++/Java/PHP code in the editor
else if Chains
Multiple conditions can be chained with else if, checked in order from top to bottom. The first matching branch runs, and the rest are skipped.
Example: else if Chains
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `grade`, set to `85`
let grade = 85;
// Check whether `grade >= 90`
if grade >= 90 {
// Print "A" to the console (with a trailing newline)
println!("A");
// Otherwise, if `grade >= 80`, run this branch
} else if grade >= 80 {
// Print "B" to the console (with a trailing newline)
println!("B");
// Otherwise, run this branch
} else {
// Print "C or below" to the console (with a trailing newline)
println!("C or below");
}
}
Login to try C/C++/Java/PHP code in the editor
if as an Expression
Because if is an expression, not just a statement, it can produce a value directly, which you can assign to a variable. Both branches must return values of the same type.
5 and 6 -- they are the tail expressions of each block.Example: if as an Expression
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `condition`, set to `true`
let condition = true;
// Declare an immutable variable `number`, set to `if condition { 5 } else { 6 }`
let number = if condition { 5 } else { 6 };
// Print "number is {}" to the console, substituting in `number` (with a trailing newline)
println!("number is {}", number);
}
Login to try C/C++/Java/PHP code in the editor
Condition Must Be a Bool
Rust requires the if condition to be an actual bool value; it will not implicitly treat a nonzero integer as true, unlike C. This prevents an entire category of bugs from mistyped conditions.
Example: Condition Must Be a Bool
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `count`, set to `3`
let count = 3;
// Check whether `count != 0`
if count != 0 {
// Print "count is non-zero: {}" to the console, substituting in `count` (with a trailing newline)
println!("count is non-zero: {}", count);
}
}
Login to try C/C++/Java/PHP code in the editor
- Wrapping the condition in parentheses like
if (x > 5), which is unnecessary and unidiomatic in Rust. - Forgetting that
ifis an expression and can return a value, then writing overly verbose code with extralet mutvariables. - Mixing types between the
ifandelsebranches when usingifas an expression, which fails to compile.
if/else if/elsebranch on boolean conditions; no parentheses are required around the condition.ifis an expression in Rust, so it can produce a value that is assigned directly to a variable.- All branches of an
ifused as an expression must evaluate to the same type. - The condition must be a
bool-- Rust does not implicitly convert numbers to booleans.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: