'Matching String for Debug and Release Build in Rust
Assuming I have an &str named myvar which I somehow get from environment variables. I can match it as below:
match myvar {
"foo" => todo!(),
"bar" => todo!(),
_ => todo!(), // if an unexpected value
}
I also can know if the compiled app is debug or release build via cfg!(debug_assertions).
Now, what I want is provide a fallback value for this &str but it should differ whether the binary is a debug build or not. So:
match myvar {
"foo" => todo!(),
"bar" => todo!(),
_ && cfg!(debug_assertions) => todo!(), // fallback in debug build
_ => todo!(), // fallback in release build
}
Is such a thing possible?
Thanks in advance.
Solution 1:[1]
The thing you're looking for is likely to be the match guards. General syntax for them is as following:
match value {
pattern if conditional = todo!(),
// other branches
}
In your case, this is a simple change:
match myvar {
"foo" => todo!(),
"bar" => todo!(),
_ if cfg!(debug_assertions) => todo!(), // fallback in debug build
_ => todo!(), // fallback in release build
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cerberus |
