4 ms·
Rust enums are full union types. i.e. while each possibility in a (say) Java enum must be of the same type, each possibility in a Rust enum can be of a differen
by renewiltord 6y ago
Rust enums are full union types. i.e. while each possibility in a (say) Java enum must be of the same type, each possibility in a Rust enum can be of a different type.
i.e.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3e9da02167dde3435735fee270b9dd24 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- DaiPlusPlus 6y agoHowever, Rust's enum-type members are still named - to my knowledge you can't have anonymous enum type members in Rust. In the example you gave, the type-of `left` is still `SumType::Left` instead of being just `String`. I'm not too familiar with Rust to say, but I don't consider this to be a syntactically zero-cost abstraction (even if the wrapper-types are elided by the compiler) because we still have more keyboard typing to do than we should be doing, imo.
- renewiltord 6y agoAh, that's to enable you to have multiple members that could hold `String` values. Yeah, it comes with the (syntactic) cost that you have to identify the member.
- ATsch 6y agoIn practice that's really not a problem because of rust's great pattern matching that easily lets you extract even deeply nested values.