Switch statements are also available in hare as well. It is important to understand they they are exhaustive so every possible results must be accounted for in the switch cases or Hare will not compile.
use fmt;
export fn main() void = {
let i = 2z;
switch (i) {
case 1z =>
fmt::println("one")!;
case 2z =>
fmt::println("two")!;
fmt::println("We have a winner!")!;
case 3z =>
fmt::println("three")!;
// Omit the value for the 'default' case which is required for
// exhaustive checking.
case =>
fmt::println("Some other number")!;
};
let dow = 3z;
switch (dow) {
// Specify many values to match against
case 1z, 2z, 3z, 4z, 5z =>
fmt::println("Weekday")!;
// We still need a 'default' value since our switch is for all
// positive integers
case =>
fmt::println("Weekend")!;
};
};
$ hare run switch.ha
4/4 tasks completed (100%)
two
We have a winner!
Weekday
Back to table of contents