4 ms·
These aren't really C style enums. These are still sum types. C-style enums are sugar for defining an integer type alias and a bunch of constants without creat
by ChrisSD 6y ago
These aren't really C style enums. These are still sum types.
C-style enums are sugar for defining an integer type alias and a bunch of constants without creating any new types or scope.
Rust enums, even with an assigned value, define a new type that can only be one of the defined values. So this type is not an integer and cannot have a bit pattern that isn't explicitly defined.
- steveklabnik 6y agoRust calls this feature "C style enums," so while you're right there are differences, it's sort of a term of art in this context. ("style" is supposed to reflect that it's similar, but not the same thing.)
- ChrisSD 6y agoYeah. I hate it ;). I already have enough trouble trying to explain to new people the difference between C and Rust enums. The phrase "C style" just confuses things more, IMHO. Admittedly this may be a failure of communication on my part.
- steveklabnik 6y agoThat's fair :) I don't think the name was super carefully considered, to be honest, it just kinda happened.
- drran 6y agoC enums in Rust can be declared as: enum Foo { A=0, B=1, C=3, _UNKNOWN(i32), } However, this cannot be compiled by current Rust compiler.