Early-breaking from Rust's match

后端 未结 3 1551
死守一世寂寞
死守一世寂寞 2021-01-05 05:01

I want to switch through many possible cases for x and there\'s one case (here x == 0) where I want to check the result of some additional code to

3条回答
  •  一生所求
    2021-01-05 05:53

    You could create a macro like

    macro_rules! block {
        ($xs:block) => {
            loop { let _ = $xs; break; }
        };
    }
    

    and do

    match x {
        1 => block!({
            ...
            if y == 0 { break; }
            ...
        })
        _ => {}
    }
    

    It's not an amazing solution, but it is semantically meaningful.

提交回复
热议问题