Chapter 12: Rust If .. Else

Rust If .. Else — one of the most fundamental ways to make decisions in code.

In Rust, we call them if expressions (not just statements) because they can return values — that’s a very nice Rust feature compared to many other languages. I’ll explain everything slowly like your personal teacher sitting next to you with VS Code open: basic syntax, chaining, else if, if as expression, common patterns, and even the related if let and let else (which are super useful in real Rust code).

Let’s go step by step with examples you can copy-paste right now.

1. Basic if – No Parentheses Needed!

Rust’s if is clean — no mandatory () around the condition, and the condition must be a bool (no automatic conversion from int like in C or JavaScript).

Rust

Run → if condition true → prints message. If false → nothing happens.

2. Adding else

Rust
  • else block runs when condition is false
  • Both blocks are {} — even single line needs them (no dangling else ambiguity)

3. Chaining with else if (Multiple Conditions)

Rust
  • Checked from top to bottom
  • Only first true block runs
  • Final else (optional) catches everything else

4. if is an Expression — It Can Return a Value!

This is huge — if can be used on the right side of let, like a ternary operator but more powerful and readable.

Rust
  • All branches must return the same type (or compatible)
  • Last expression in each block is the value (no ; needed if it’s the return)

Example with numbers:

Rust

5. if let – Pattern Matching in if (Very Common & Clean)

if let combines if + let for when you only care about one pattern (usually Some / Ok / specific enum variant).

Rust
  • Shorter than full match when you ignore most cases
  • Can chain else if let too (but less common)

6. let else – The Modern Way (Rust 1.65+ — Very Useful 2026)

Introduced later — lets you bind variables or diverge (return/panic/break) early if pattern fails.

Rust
  • let else binds if match succeeds
  • else block must diverge (never returns normally) — compiler enforces this
  • Cleaner than old match or if let + return

Real example (file reading style):

Rust

7. Quick Tips & Common Mistakes (Teacher Warnings)

  • No () around condition → if temperature > 32 (not if (temperature > 32))
  • Braces {} always required — even for one line
  • All branches same type when used as expression
  • Prefer if let / let else over deep match when only one case matters
  • For many conditions → match often cleaner than long else if chain

Full Small Project Example to Try

Create cargo new rust_if_else and put in main.rs:

Rust

Run cargo run — play with changing hour, maybe_temp, input values.

Summary in Teacher Voice

Rust if .. else = powerful decision making:

  • Basic if {} / else {} / else if {}
  • No parentheses needed around condition
  • if is an expression — can assign to variables
  • if let = concise for one pattern match + else
  • let else = bind or early exit (modern favorite)

This + match covers 95% of control flow needs before loops.

Next ready?

  • Loops (loop, while, for)?
  • match expression (Rust’s superpower)?
  • Or functions with if inside?

Just tell me — your Rust class is going strong! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *