What is the best way to use a value computed at runtime as one of the patterns in a match?
I have a value (byte
) that I need to match against other values. Some of the values are fixed (b'0'..b'9'). Others are computed at runtime (c = some_function()
).
My current solution is to use a fake variable and a if guard (i.e. k if (k == c)
), but it does not look very nice to me. I tried using just c
but that gets interpreted as a catch-all variable, rather than substituted for the value of c
in that context.
The following code snippet shows the problem: (also on playpen)
fn main() {
fun(b'5', 0);
fun(b'C', 0);
fun(b'C', 2);
}
fn fun(byte: u8, i: uint) {
let CHARS = b"ABCDEFGH";
let c = CHARS[i];
let msg = match byte {
b'0'..b'9' => "numeric",
// c => "same char", // <-- I would had preferred this
k if (k == c) => "same char",
_ => "different char",
};
println!("fun({}, {} [{}]) = {}", byte, i, c, msg);
}
Is this the most idiomatic construct that Rust can offer?
In short, yes, you should use pattern guards for this.
To make it possible there should be a way to differentiate plain bindings and equality checks. Scala, for example, does this based on variable case: if it starts with capital letter, it is an equality check; otherwise it is a pattern binding. There is no such mechanism in Rust, so no, it is impossible now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With