-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[r+] Add enum discriminants to the reference. #21047
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
@@ -1413,6 +1413,21 @@ a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 }; | |||
In this example, `Cat` is a _struct-like enum variant_, | |||
whereas `Dog` is simply called an enum variant. | |||
|
|||
Enums can have a discriminator: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums always have a discriminant, this syntax just lets you specify exactly what it is. They'll default to 0, 1, 2, ...
@sfackler addressed |
If a discriminant isn't assigned, they start at zero, and add one for each | ||
variant, in order. | ||
|
||
You can cast an enum to get this value: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last nit: you can only do this if the enum is C-like, i.e. none of the variants have any data attached. For example, you can't do None as u32
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C-like, i.e. none of the variants have any data attached.
By "C-like" you mean none like Foo(i32)
, yes? They all need to be Foo
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! PR #20907 adds an intrinsic to extract he discriminant from any enum, but its only intended to be used as an optimization for things like PartialEq
implementations.
r+ with @sfackler 's nit addressed |
[r+] Add enum discriminants to the reference. Reviewed-by: nikomatsakis
[r+] Add enum discriminants to the reference. Reviewed-by: nikomatsakis
Doing this manually as part of #21300 |
Fixes #15755