I'm writing a compiler that compiles to C, one thing I'm attempting to do is implement a construct like Rust's match:
// { some function
let mut foo = 32;
match foo {
3 => return "hey",
4 | 5 | 6 => return "foo",
9 ... 12 => return "blah",
_ => return "bar",
}
// }
How would a construct like this look in C code? I was thinking something simple like this could be a switch construct like so:
switch (foo) {
case 3: return "hey";
case 4: case 5: case 6: return "foo";
case 9: case 10: case 11: case 12: return "blah";
default: return "bar";
}
And most importantly, how would it look for more complicated examples like this:
let pair = (0, -2);
match pair {
(0, y) => println!("First is `0` and `y` is `{:?}`", y),
(x, 0) => println!("`x` is `{:?}` and last is `0`", x),
_ => println!("It doesn't matter what they are"),
}
Where the tuple is destructured and can handle values too?