4 ms·
> You can assign numeric values to enum variants in Rust too Is this (C-style enums) new? I swear I looked for it a year or two back, found questions about it
by alkonaut 6y ago
> You can assign numeric values to enum variants in Rust too
Is this (C-style enums) new?
I swear I looked for it a year or two back, found questions about it, with the answers "use constants".
- ChrisSD 6y agoThese 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.
- nicoburns 6y agoEDIT: Found it in the changelog. This feature was added in Rust 0.8 (2013-09-26) "Explicit enum discriminants may be given as uints as in enum E { V = 0u }" https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-08-2013-09-26 https://github.com/rust-lang/rust/blob/master/RELEASES.md#ve... They're referenced as a pre-existing feature in this Jan 2015 RFC [0], so they predate Rust 1.0. I don't know quite how old they are, but I suspect much older than that as this feature is necessary for interfacing with C which has been a high priority for Rust for a long time (particularly driven by the needs of the Servo project in the early years). [0]: https://rust-lang.github.io/rfcs/0639-discriminant-intrinsic.html https://rust-lang.github.io/rfcs/0639-discriminant-intrinsic...
- masklinn 6y ago> Is this (C-style enums) new? No. > I swear I looked for it a year or two back, found questions about it, with the answers "use constants". Possibly because they were asked / answered in the context of bit flags? Even degenerate, rust enums are not compatible with C enums because they're still type-safe, you can't create any random value and say "that's a Foo", that'd be UB. So if you want bitflags, you need to use constants.
- alkonaut 6y agoThanks. I didn't want bitflags, this was just basically for an enumeration of channels/parameters e.g Foo=0, Bar=1, Baz=2. Ideally I'd like one that was numbered automatically and also offered a way of enumerating the possible values at runtime. I can't remember why I went back to constants in that case. Another struggle I remember was just getting the max defined value. Again the Rust type is pretty Opaque because it hides the all-powerful discriminated union type. I love the Rust enum type, but I wish Rust also had a first-class dumb enum type, with all the C/C# niceties (bitflags, runtime enumeration, selection of backing field). This seems like quite a different from what the rust enum is.
- steveklabnik 6y agoYou probably ran into this stuff not being built-in. You can add it on via a macro pretty easily, except that the "selection of backing field" is built in via the repr attribute. An example of this would be packages like https://docs.rs/enum_derive/0.1.7/enum_derive/ https://docs.rs/enum_derive/0.1.7/enum_derive/ The other stuff isn't built in because it would add a bunch of code you may or may not want to your build, and it's easy enough to opt into. We also could solve that problem, in theory, a few ways, but there just hasn't been a ton of demand for it, so it hasn't been done. With bitfields, there has been increasing demand lately, so we'll see.
- alkonaut 6y agoI constantly run into the situation where you make 2 or more constants with a prefix. That always felt like a smell to me - but it might be because in C# it would be. I'm talking about code that ends up looking like this: https://github.com/retep998/winapi-rs/blob/0.3/src/shared/ntstatus.rs https://github.com/retep998/winapi-rs/blob/0.3/src/shared/nt... You get type safety because you can define the type of constant rather than use some primitive, but there feels like something is something missing (nicer autocompletion, better auto generated documentation, and so on).