I have an enum, it looks like this:
pub enum Boop {
Here(...),
There(...),
// ...
}
I have some other code, it looks like this:
fn do_something(boop: Boop) {
// ...
let Boop(here) = match boop { Here(h) => h, panic("Not a 'Here'") };
// alternatively:
// if let Boop::Here(h) = here { /* ... */ } else { panic!("Not a 'Here'") }
// ...
}
The thing is, the Boop
passed to do_something
should always be the variant Boop::Here
- if it's anything else, there is something wrong with the code itself, wherein a panic is justified. However, this doesn't feel like idiomatic code. Would there be a more idiomatic way or general method to do the above? Is it possible to check that boop
is always a Boop::Here
?
TL;DR: How do you ensure that an enum is always a specific variant?