7.1 Conditional Statements
Conditional statements control whether a block of code executes based on a boolean condition. Rust’s if
, else if
, and else
constructs will look familiar to C programmers, but there are some important differences.
7.1.1 The Basic if
Statement
The simplest form of Rust’s if
statement looks much like C’s:
fn main() { let number = 5; if number > 0 { println!("The number is positive."); } }
Key Points:
- No Implicit Conversions: The condition must be a
bool
. - Parentheses Optional: Rust does not require parentheses around the condition (though they are allowed).
- Braces Required: Even a single statement must be enclosed in braces.
In Rust, the condition in an if
statement must explicitly be of type bool
. Unlike C, where any non-zero integer is treated as true
, Rust will not compile code that relies on integer-to-boolean conversions.
C Example:
int number = 5;
if (number) {
// In C, any non-zero value is considered true
printf("Number is non-zero.\n");
}
7.1.2 if
as an Expression
One noteworthy difference from C is that, in Rust, if
can be used as an expression to produce a value. This allows you to assign the result of an if
/else
expression directly to a variable:
fn main() { let condition = true; let number = if condition { 10 } else { 20 }; println!("The number is: {}", number); }
Here:
- Both Branches Must Have the Same Type: The
if
andelse
blocks must produce values of the same type, or the compiler will emit an error. - No Ternary Operator: Rust replaces the need for the ternary operator (
?:
in C) by lettingif
serve as an expression.
7.1.3 Multiple Branches: else if
and else
As in C, you can chain multiple conditions using else if
:
fn main() { let number = 0; if number > 0 { println!("The number is positive."); } else if number < 0 { println!("The number is negative."); } else { println!("The number is zero."); } }
Key Points:
- Conditions are checked sequentially.
- Only the first matching
true
branch executes. - The optional
else
runs if no preceding conditions match.
7.1.4 Type Consistency in if
Expressions
When using if
as an expression to assign a value, all possible branches must return the same type:
fn main() { let condition = true; let number = if condition { 5 } else { "six" // Mismatched type! }; }
This code fails to compile because the if
branch returns an i32
, while the else
branch returns a string slice. Rust’s strict type system prevents mixing these types in a single expression.