On Fri, Oct 2, 2020, 10:53 Jason Ekstrand <ja...@jlekstrand.net> wrote:

>
>  2. Rust's enums look awesome but are only mostly awesome:
>     a. Pattern matching on them can lead to some pretty deep
> indentation which is a bit annoying.
>     b. There's no good way to have multiple cases handled by the same
> code like you can with a C switch; you have to either repeat it or
> break it out into a generic helper.
>

You can use | in match which could help with 2.b:
https://doc.rust-lang.org/reference/expressions/match-expr.html
example:
match make_my_enum() {
    B { strv: s, .. } | C(MyStruct(s)) => println!("B or C: the string is
{}", s),
    A(_) => {}
}
struct MyStruct(String);
enum MyEnum {
    A(String),
    B {
        intv: i32,
        strv: String,
    },
    C(MyStruct),
}

Jacob Lifshay
_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to