Rust if / else Explained: Conditions, else if and if as an Expression
November 10, 2026
Welcome to episode 5 of Rust for Beginners, a series for complete beginners (no programming experience needed). In the last video, we learned about functions. Today, we'll learn how our programs make decisions, with if and else. Loops get their own video next time. Every code block below is the exact code from the video, ready to copy.
- All episodes are in the Rust Tutorial for Beginners playlist.
- Versions used: Rust 1.99.1 on Ubuntu, VS Code with rust-analyzer.
1. New project
Let's create a new project called conditions, and open it in VS Code.
cargo new conditions
cd conditions2. if
Here's a temperature of thirty. If the temperature is greater than twenty-five, we print "It's hot today!". The condition goes after if, with no brackets around it, and the code runs only when it's true.
src/main.rs:
fn main() {
let temperature = 30;
if temperature > 25 {
println!("It's hot today!");
} else {
println!("It's not that hot.");
}
}Output: It's hot today!
Then line 4 becomes if temperature {. Error:
error[E0308]: mismatched types
--> src/main.rs:4:8
|
4 | if temperature {
| ^^^^^^^^^^^ expected `bool`, found integer3. else
Otherwise, the else block runs.
4. Run A
Let's run it. Thirty is more than twenty-five, so it's hot today.
cargo run5. Bool only
In some languages, you can write if temperature, and any number that isn't zero counts as true. Rust doesn't allow that. The condition must be a bool.
cargo run6. Fix
Let's put the comparison back.
7. else if
To check more than two cases, we add else if, one after another. Rust checks them from top to bottom, and runs only the first one that's true.
fn main() {
let score = 72;
if score >= 90 {
println!("Grade: A");
} else if score >= 70 {
println!("Grade: B");
} else if score >= 50 {
println!("Grade: C");
} else {
println!("Grade: F");
}
let homework_done = true;
if score >= 70 && homework_done {
println!("Great job!");
}
}Output: Grade: B / Great job!
8. And
We can also combine conditions. Two ampersands mean "and": both sides must be true. Two vertical bars mean "or".
9. Run B
A score of seventy-two isn't ninety or more, but it is seventy or more, so the grade is B. And the homework is done, so: great job!
cargo run10. Expression
In Rust, if is an expression: it gives back a value. So we can put it on the right side of let.
fn main() {
let age = 20;
let status = if age >= 18 { "adult" } else { "child" };
println!("Status: {status}");
println!("Bigger number: {}", max(8, 3));
}
fn max(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}Output: Status: adult / Bigger number: 8
Then { "child" } becomes { 17 }. Error:
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:3:50
|
3 | let status = if age >= 18 { "adult" } else { 17 };
| ------- ^^ expected `&str`, found integer
| |
| expected because of this11. No ternary
That's why Rust has no question-mark-colon operator like other languages. if does the job.
12. In a function
It's great as the last expression of a function. Here, max returns a if it's bigger, otherwise b.
13. Same type
Both branches must give the same type. What if the else branch gives a number?
cargo run14. Error
Rust says if and else have incompatible types: it expected a string, but found an integer. A variable can only have one type.
15. Run C
Let's put "child" back, and run.
cargo runRecap
To recap: the condition must be a bool, else if checks more cases, and if is an expression, so both branches must have the same type.
16. `&& and · \
\