Introduction
In Rust, if is not just a statement — it is an expression that evaluates to a value. This means you can use an if-else block directly on the right-hand side of a let binding, avoiding the need for a separate temporary variable. Unlike languages such as C or JavaScript, Rust does not allow implicit conversion from integers or other types to booleans, so the condition in an if must be an actual bool value.
Cricket analogy: Just as a match referee's decision directly becomes the recorded outcome rather than a separate note, Rust's if-else evaluates directly into a let binding; and like DRS requiring an actual out/not-out signal, Rust requires a real bool, not a runs count treated as true.
Syntax
if condition {
// executes when condition is true
} else if other_condition {
// executes when other_condition is true
} else {
// executes otherwise
}
// as an expression
let result = if condition {
value_a
} else {
value_b
};Explanation
Rust does not require parentheses around the condition, but the curly braces around each branch are mandatory — this removes the classic 'dangling else' ambiguity found in C-like languages. Because if-else is an expression, every branch used in a value-producing position must return the same type; the compiler rejects mismatched branch types at compile time. When if is used purely for side effects (no assignment), the branches typically evaluate to the unit type ().
Cricket analogy: Rust doesn't need parentheses hugging the over-count like some scorebooks do, but mandatory braces around each innings prevent the "dangling appeal" ambiguity of which bowler a decision applies to; every batting-order branch must return the same run-type, and a pure signal like a wicket celebration resolves to nothing, the unit type.
Example
fn main() {
let temperature = 15;
let description = if temperature > 30 {
"hot"
} else if temperature > 15 {
"warm"
} else {
"cool"
};
println!("It feels {}", description);
// condition must be bool, this would NOT compile:
// if temperature { ... }
}Output
It feels coolKey Takeaways
- if in Rust is an expression, so it can produce a value usable in let bindings.
- The condition must be a bool — Rust has no implicit truthy/falsy conversion.
- Braces around branches are required; parentheses around the condition are optional.
- All branches used as an expression must evaluate to the same type.
- else if chains let you express multiple mutually exclusive conditions cleanly.
Practice what you learned
1. What type must the condition in an if statement be in Rust?
2. Which statement about if in Rust is true?
3. What happens if the branches of an if-else expression return different types and the result is assigned to a variable?
4. Are parentheses required around the condition of an if statement in Rust?
Was this page helpful?
You May Also Like
Loops in Rust (loop, while, for)
Understand Rust's three loop constructs — loop, while, and for — and how loop can return a value with break.
match Expressions in Rust
Master Rust's exhaustive match expression for pattern matching on values, enums, tuples, and ranges.
if let and while let in Rust
Simplify single-pattern matching in Rust using the concise if let and while let constructs.
Variables and Data Types in Rust
Learn how Rust variables are immutable by default and explore its scalar and compound data types.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics