I have code that requires enums to be a certain type before continuing (and if they are not then it is an unrecoverable error). Expressing this with the if let pattern is leading to a lot of indentation and syntax noise. Here's an example:
enum Fruit {
Apple(i32),
Banana(i32, i32),
}
enum Veggie {
Tomato(bool),
Pepper(i32),
}
fn test() -> i32 {
let fruit = fruit_producing_func();
if let Fruit::Apple(x) = fruit {
let veggie = veggie_producing_func(x);
if let Veggie::Pepper(y) = veggie {
y
} else {
panic!();
}
} else {
panic!();
}
}
In my code this nesting gets much larger than 2 layers. Is there a more concise way to express this which avoids creating extra blocks, or even the repetition of the panic! statements? Simply doing:
let Fruit::Apple(x) = fruit;
leads to an error because let bindings have to be exhaustive (I was hoping it would just panic on failed match).