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?
switchstatement with this same functionality, and it is spelled mostly like the Cswitch. Here's the doc: developer.apple.com/library/prerelease/content/documentation/…matchisn't the only place these can happen in Rust, e.g.let (a,b) = (1,2);. Solving the simpler situations might make the solution formatchmore obvious. Knowing how you're implementing tuples and other complex data types may also help.