Skip to content

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).


  • Treat if/else and match as expressions that produce values, replacing the ternary ? : and switch
  • Write conditions as explicit bool tests — Rust has no truthiness, so if count, if name, and if 0 are all compile errors
  • Iterate over ranges (0..n, 1..=n) and collections instead of a C-style for counter, which Rust does not have
  • Use the dedicated infinite loop and return a value from it with break value
  • Replace switch with match: exhaustiveness, the _ catch-all, guards, | alternatives, ranges, @ bindings, and destructuring — with no fall-through
  • Reach for the lightweight if let, while let, and let ... else when only one case matters
  • Control nested loops with labeled loops ('outer:) and understand how they differ from JavaScript labels

#TopicWhat it covers
1Conditionalsif/else; the ternary becomes if as an expression; no truthiness (conditions are bool only); an if let teaser
2Loopsfor over ranges/iterators, while, the infinite loop; loop returning a value via break; why there is no C-style for
3matchswitch becomes match: exhaustiveness, the _ arm, guards, | patterns, ranges, the @ binding, and destructuring
4if let / while letConcise pattern matching: if let / else, while let, and let ... else for early-return guard clauses
5break and continuebreak/continue; break carrying a value out of a loop; continue in for and while
6Labeled LoopsLabeled loops ('outer:); break/continue to a label; nested-loop control versus JavaScript labels

After completing this section, a TypeScript/JavaScript developer should be able to:

  1. Bind the result of an if/else or match directly to a let, and explain why both arms must unify to a single type.
  2. Rewrite any truthiness-based condition (if (x), while (queue.length)) as an explicit bool test (if x != 0, while !queue.is_empty()).
  3. Translate a C-style for (let i = 0; i < n; i++) into a range loop, and use .enumerate() when an index is genuinely needed.
  4. Produce a value from a loop with loop { ... break value; } instead of a pre-declared mutable variable.
  5. Replace a switch with an exhaustive match, using |, ranges, guards, @, and destructuring — and know why match never falls through.
  6. Choose the lightest pattern-matching tool: if let, while let, let ... else, or a full match.
  7. Use break 'label / continue 'label to control nested loops, and recognize when an iterator method is clearer.

This section assumes you have completed:

  • Section 03: Functions — the fn signature, 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 makes if, match, and loop usable 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.


  • 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 → matchif let/while letbreak/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.