Switch / Match

C++

A switch statement in C or C++ allows a condition or a variable to be compared to a series of values and for code associated with those values to executed as a result. There is also a default clause to match any value that is is not caught explicitly.

  1. int result = http_get();
  2. switch (result) {
  3. case 200:
  4. success = true;
  5. break;
  6. case 404:
  7. log_error(result);
  8. // Drop through
  9. default:
  10. success = false;
  11. break;
  12. }

Switch statements can be a source of error because behaviour is undefined when a default clause is not supplied. It is also possible to inadvertently forget the break statement. In the above example, the code explicitly “drops” from the 404 handler into the default handler. This code would work fine providing someone didn’t insert some extra clauses between 404 and default…

Additionally switch statements only work on numeric values (or bool).

Rust

Match is like a switch statement on steroids.

In C++ a switch is a straight comparison of an integer value of some kind (including chars and enums), against a list of values. If the comparison matches, the code next to it executes until the bottom of the switch statement or a break is reached.

TODO