Chapter 13: Rust Match

Rust match — perfect timing because match is basically Rust’s super-powered upgrade to switch/if-else chains.

In Rust, match is one of the most loved and most powerful features. People call it “pattern matching on steroids.” It lets you compare a value against many patterns, run code for the first matching one, and the compiler forces you to handle every possible case (exhaustive checking — huge safety win!).

Think of it like this:

  • In other languages (C, Java, JavaScript): switch is limited — only integers/strings, no destructuring, fall-through bugs possible.
  • In Rust: match works on anything (enums, structs, tuples, ranges, literals, variables…), destructures values, binds variables, has guards, and never forgets a case.

Let me explain it like your patient teacher sitting next to you with code open: slowly, with many real examples (copy-paste ready), Hyderabad-flavored analogies, and step-by-step building from simple to advanced.

1. Basic match Syntax

Rust
  • match is an expression → can return a value (like if)
  • All arms must return the same type (or () if no value)
  • Arms separated by ,
  • Last arm can omit , if it’s a block

2. Simple Example — Like switch on a number

Rust
  • = or (match any of these)
  • ..= = inclusive range (13 to 19)
  • _ = catch-all wildcard (must be last if used)

Run → try changing number to 13, 4, 1, 42 — see different arms fire.

3. Match is an Expression → Returns Values

Rust

No return needed — last expression in arm is the value.

4. Matching Enums (The Real Power — Why Rust Loves match)

Enums + match = killer combo (like the coin example from The Book).

Rust
  • Destructures Quarter(state) → binds state variable
  • Compiler error if you forget any variant → exhaustive!

5. Destructuring Structs & Tuples

Rust
  • Point { x: 0, y } → literal match on x, bind y
  • Shorthand: Point { x, y } = bind both with same name

6. Guards (if conditions on patterns)

Rust
  • n if … = guard — only matches if condition true
  • n = bound variable (can use in guard)

7. Matching References & Option/Result (Very Common)

Rust

8. Advanced but Useful: @ Bindings & OR Patterns

Rust
  • @ = bind whole pattern to name

Teacher Summary – Why match is Special in Rust

Rust match =

  • Exhaustive (compiler forces all cases) → no forgotten enum variants
  • Destructuring + binding + guards + ranges + or-patterns
  • Expression → returns values cleanly
  • Safe alternative to many if-else chains
  • Heart of Rust error handling (match on Result, Option)

Practice tip: Create cargo new rust_match Try rewriting some if-else from earlier examples as match Then make an enum for Hyderabad food (Biryani, Haleem, Irani Chai…) and match on spiciness level.

Next ready?

  • Loops (loop, while, for)?
  • Ownership with match (moves, refs)?
  • Or Option & Result in depth (where match shines most)?

Just say — your Rust guru from Hyderabad is here! 🦀🚀

You may also like...

Leave a Reply

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