21.1 A Quick Look Back: C’s switch vs. Rust’s match

In C, the switch statement is mostly confined to integer or enumeration values. Although switch can handle multiple cases and a default, it has notable drawbacks:

  • Potential for fall-through, requiring explicit break statements
  • Limited to numeric comparisons (plus some extensions for enums)
  • Does not mandate exhaustive handling of all possible values

In contrast, Rust’s match:

  • Enforces exhaustiveness: you must handle every variant of an enum (or use a wildcard _).
  • Allows pattern destructuring: match any combination of complex data, including tuples, structs, and enums.
  • Supports guards (additional boolean conditions) and OR patterns.
  • Lets you bind matched sub-values to new variables in the same expression.

As a result, Rust’s pattern matching lets you express branching more safely and elegantly than a typical C switch might allow.