Python Conditionals & If as an Expression
In Rust, if conditions must be explicit booleans (no automatic truthy/falsy coercion). Because if is an expression, you can assign its result directly to a let binding: let x = if cond { 5 } else { 10 };.
"Because if is an expression in Rust, it acts like an automated vending chute: pick button A and it dispenses item 1; pick button B and it dispenses item 2 directly into your basket."
Deep Dive: How It Works
Strict Booleans: Conditions must evaluate strictly to bool (e.g. if count != 0, not if count).
If in let Bindings: Both if and else arms must evaluate to the exact same type.
No Parentheses Required: Rust does not require parentheses around if conditions.
Syntax Blueprint
let status = if score >= 90 { "Pass" } else { "Review" };Assigns the resulting branch expression directly into the status variable binding.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Returning different types from if and else arms in a let binding.Why it happens: let x = if true { 5 } else { "five" }; fails because types must match at compile time.
How to fix: Ensure all branches evaluate to the exact same type.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Evaluate system load with if expression
Given let load_percent = 85;
Use an if expression to set let status = if load_percent > 80 { "HIGH" } else { "NORMAL" };
Print "System Status: HIGH".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.