Polyglot CheatSheet - Switch

Updated: 2021-11-19

C++

switch(var) {
  case 1:
      do_somthing_1();
      break;
  case 2:
      do_somthing_2();
      break;
  default:
      do_somthing_else();
}

Rust

Rust uses match keyword for pattern matching, which can be used like switch in other languages.

match var {
    1 => do_somthing_1(),
    2 => do_somthing_2(),
    _ => do_somthing_else(),
}

Java

switch (var) {
  case 1:
      doSomthing1();
      break;
  case 2:
      doSomthing2();
      break;
  default:
      doSomthingElse();;
}

More details: Java switch