Match statement chaining

63 points EmbarrassedWallaby3

Hello here!

I use rust since more than 1 year now, and I'm often facing issues of that sort:

enum EnumA {
    A(u32),
    B(u32, u32),
    C { p1: u32, p2: u32 },
}

enum EnumB {
    A(u32),
    B(u32, u32),
    C { p1: u32, p2: u32 },
}

pub fn main() {
    let a = EnumA::A(10);
    let b = EnumB::A(20);

    let c = match a {
        EnumA::A(va) => match b {
            EnumB::A(vb) => Some(va + vb),
            _ => None,
        },
        EnumA::C { p1, .. } => match b {
            EnumB::C { p2, .. } => Some(p1 + p2),
            _ => None,
        },
        _ => None,
    };
}

I have this ugly match chaining polluting my code. And I wonder if there was in rust something to fix this. I know you can use match guard with the matches! macro if you don't need destructuring. But is there a more proper way than that if you want to destructure ? Or if there's a crate which do something like that.