Overview
5 min read
You already write conditionals, loops, and switch statements every day in TypeScript and JavaScript. Rust gives you the same building blocks — if/else, for/while/loop, and the match expression — but with one defining shift: Rust control flow is expression-based, not statement-based. An if, a match, and even a loop each evaluate to a value, so they replace the ternary, the discriminated-union switch, and the “declare a let outside the loop and mutate it” pattern all at once. Two more rules ripple through everything: conditions must be a real bool (there is no truthiness), and match is exhaustive (the compiler refuses to let you forget a case).
What You’ll Learn
Section titled “What You’ll Learn”- Treat
if/elseandmatchas expressions that produce values, replacing the ternary? :andswitch - Write conditions as explicit
booltests — Rust has no truthiness, soif count,if name, andif 0are all compile errors - Iterate over ranges (
0..n,1..=n) and collections instead of a C-styleforcounter, which Rust does not have - Use the dedicated infinite
loopand return a value from it withbreak value - Replace
switchwithmatch: exhaustiveness, the_catch-all, guards,|alternatives, ranges,@bindings, and destructuring — with no fall-through - Reach for the lightweight
if let,while let, andlet ... elsewhen only one case matters - Control nested loops with labeled loops (
'outer:) and understand how they differ from JavaScript labels
Topics
Section titled “Topics”| # | Topic | What it covers |
|---|---|---|
| 1 | Conditionals | if/else; the ternary becomes if as an expression; no truthiness (conditions are bool only); an if let teaser |
| 2 | Loops | for over ranges/iterators, while, the infinite loop; loop returning a value via break; why there is no C-style for |
| 3 | match | switch becomes match: exhaustiveness, the _ arm, guards, | patterns, ranges, the @ binding, and destructuring |
| 4 | if let / while let | Concise pattern matching: if let / else, while let, and let ... else for early-return guard clauses |
| 5 | break and continue | break/continue; break carrying a value out of a loop; continue in for and while |
| 6 | Labeled Loops | Labeled loops ('outer:); break/continue to a label; nested-loop control versus JavaScript labels |
Learning Objectives
Section titled “Learning Objectives”After completing this section, a TypeScript/JavaScript developer should be able to:
- Bind the result of an
if/elseormatchdirectly to alet, and explain why both arms must unify to a single type. - Rewrite any truthiness-based condition (
if (x),while (queue.length)) as an explicitbooltest (if x != 0,while !queue.is_empty()). - Translate a C-style
for (let i = 0; i < n; i++)into a range loop, and use.enumerate()when an index is genuinely needed. - Produce a value from a loop with
loop { ... break value; }instead of a pre-declared mutable variable. - Replace a
switchwith an exhaustivematch, using|, ranges, guards,@, and destructuring — and know whymatchnever falls through. - Choose the lightest pattern-matching tool:
if let,while let,let ... else, or a fullmatch. - Use
break 'label/continue 'labelto control nested loops, and recognize when an iterator method is clearer.
Prerequisites
Section titled “Prerequisites”This section assumes you have completed:
- Section 03: Functions — the
fnsignature, typed parameters, and especially the tail expression (a block’s last line, with no semicolon, becomes its value). That expression-as-value rule is exactly what makesif,match, andloopusable as values here.
If the expression-oriented model (let x = { ...; a + b };) or the statement-vs-expression distinction feels unfamiliar, revisit Section 02 — Variables and Mutability before starting.
Note: A few topics here preview concepts covered fully later.
Option<T>and the?operator are introduced in Section 08 — Error Handling; the borrow-checker rules behind “you can’t mutate a collection while iterating it” belong to Section 05 — Ownership; and the full iterator toolbox (map/filter/find/take_while) lives in Section 07 — Collections. You do not need those sections first — the links are there for when you want to go deeper.
Tip: The apostrophe in a loop label (
'outer:) is the same sigil Rust uses for lifetimes in Section 05 — Ownership. They are unrelated concepts that share punctuation; the compiler always knows which one you mean from context.
Estimated Time
Section titled “Estimated Time”- Reading: 2.5-3.5 hours
- Hands-on Practice & Exercises: 2.5-3.5 hours
- Total: 5-7 hours
A reasonable order is the list order above: conditionals → loops → match → if let/while let → break/continue → labeled loops. match (topic 3) is the conceptual heart of the section — it underpins if let, while let, and let ... else — so do not skip it.