I often find myself writen code that looks like this:
enum A {
A1(B),
A2(i8),
}
enum B {
B1(String),
B2(i16),
}
fn main() {
let mut a = A::A1(B::B1("test".to_string()));
match a {
A::A1(mut b) => {
match b {
B::B1(mut s) => s.push('c'),
_ => panic!(),
}
}
_ => panic!(),
}
}
A very convenient shorthand would be to something like this:
a.expect_A1().expect_B1().push('c');
I could do that my writing a expect_A1 and expect_B1 method, but this would require manual labor and clutter the code.
Is there some way to conveniently shorten the syntax in the first example? Is it possible to dervie these excpect_Enumvariant functions?