0

When the type of an enum is known, getting its value seems overly verbose, compared to getting the value from a box for example.

Given this function which gets self.data 's value when its type of enum is known:

fn enum_known_type_cast(&self) -> &SomeValue {
    match self.data {
        MyEnum::EnumKind(ref value) => {
            return value;
        }
        _ => {
            unreachable!();
        }
    }
}

Can it be simplified to something like this?

fn enum_known_type_cast(&self) -> &SomeValue {
    match self.data.enum_unwrap_mut<MyEnum::EnumKind>();
}
ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • FWIW, your second ` unreachable!()` shouldn't be required, as `_` should match all the other possibilities. – Shepmaster Jan 23 '17 at 23:52
  • And then it can be something like `match self.data { MyEnum::EnumKind(ref value) => value, _ => unreachable!() }` which isn't terrible. Or the `if let` from the duplicate. – Shepmaster Jan 24 '17 at 00:05

0 Answers0