By the end of this lesson
Choose a branch with a boolean condition and return a value from if.
Before you start
- Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
- Complete lesson 6, or be comfortable explaining and running its examples.
Three words to know
- Boolean
- A true or false value.
- Branch
- One path through a decision.
- Exhaustive
- Covering every possible case.
1. Understand the idea
An if condition must be bool; Rust does not treat arbitrary numbers or strings as true. Both branches of an if expression must produce compatible types. Put the condition in words before coding it: this helps distinguish greater than from greater than or equal to. match will later help us cover richer sets of possible values.
2. Save, run and observe
Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.
fn main() {
let pending = 3;
let label = if pending == 0 { "empty" } else { "work remains" };
println!("{label}");
}
What you should see · example output
work remains
rustc --edition=2024 lesson.rs -o lesson
./lesson3. Explain the result
pending is compared to zero; the comparison produces bool. Each branch returns &str so the if expression has a consistent type. A single equals sign would be assignment, not equality. Test both zero and a positive value when deciding whether this label works.
YOUR TURN
A small check before you continue
Change pending to zero. What prints?
Show a suggested answer
empty followed by a newline. The equality check becomes true.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
