4 ms·
C has enums, too, you know. What do Rust enums do that C enums don't?
by msla 3y ago
C has enums, too, you know. What do Rust enums do that C enums don't?
- cesarb 3y ago> What do Rust enums do that C enums don't? In Rust, an enum can also carry data along with the tag, and the type of the data can be different for each value of the tag. A Rust enum is more like a "tagged union" (a struct containing both an enum as the tag and a union for the extra data) in C.
- rgoulter 3y agoRust's enums are "sum types" or "tagged unions". Perhaps the most practical example of this is the Option type, where you can say an Optional value is either `Some(value)` or `Nothing`. With this, you don't have do deal with null pointers; a `null` pointer would instead be `Nothing`, and a non-null value would be `Some(value)`, and the compiler can enforce you're sticking to this.
- jerf 3y agoI wish "enums" would stay relatively simple numbers or something and "sum types" would remain full sum types, but, here we are. Now whenever I see someone asking for "enums" I have to ask them what they're actually saying, because the majority of the time they mean sum types, but only the majority. It is not even close to always. Sometimes people want more fully featured enumerations, too, e.g., better automatic stringification of a variety of types.
- _dain_ 3y agoexhaustiveness checking
- leoedin 3y agoIt's a reasonable question. Aside from the nice data encapsulation of enums (which make them more like unions), the answer is basically "they're enforced". Most C libraries and system calls return integers not enums. Often the meaning of those integers is hard coded in a header somewhere. There's no guarantee that the number returned will be one of the values in the header. When a library does return enums, there's no guarantee the value returned is actually in the enum. It could be anything. When you pattern match an enum in C, you have no checks to make sure you catch all the cases, or don't drop a break;. All of the above means that in C, you're relying on the programmer to cross check they haven't made a mistake. Rust does it all for you - if you match against an enum type, you have to handle all the cases. You can't return anything other than the type you said you would. It basically does all the work that the programmer has to do in C. And most importantly, it does it every time you recompile. Even if the first person to call a function in C is meticulous, the members in the enum might change later.
- uecker 3y agoYou could have similar warning in C compilers: https://godbolt.org/z/aj6TWsxK3 https://godbolt.org/z/aj6TWsxK3 I agree that Rust has more comprehensive guard railes, but except for pointer ownership and sharing where C is missing tools (although model checking tools can achieve this too), I think you could basically have everything else in C as well by building similar abstractions building on existing compiler warnings and maybe adding a couple of new ones. So while I like some ideas of Rust, I think in the discussions proponents often compare it to a worst-case C without old programming style and no static analysis applied and comparing to an ideal Rust with no use of unsafe (and also this binder rewrite uses unsafe).
- kajaktum 3y agoBut C does not have algebraic enums. Try doing JSON in C and see how that goes. Every function now returns an int and a void*; go ahead and make sure they are always paired together.