Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

steveklabnik
Copy link
Member

Fixes #15755

@rust-highfive
Copy link
Contributor

r? @nikomatsakis

(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:
Copy link
Member

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, ...

@steveklabnik
Copy link
Member Author

@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:
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

@nikomatsakis
Copy link
Contributor

r+ with @sfackler 's nit addressed

@nikomatsakis nikomatsakis changed the title Add enum discriminats to the reference. r+] Add enum discriminants to the reference. Jan 14, 2015
@nikomatsakis nikomatsakis changed the title r+] Add enum discriminants to the reference. [r+] Add enum discriminants to the reference. Jan 14, 2015
bors added a commit that referenced this pull request Jan 17, 2015
[r+] Add enum discriminants to the reference.

Reviewed-by: nikomatsakis
bors added a commit that referenced this pull request Jan 17, 2015
[r+] Add enum discriminants to the reference.

Reviewed-by: nikomatsakis
@steveklabnik
Copy link
Member Author

Doing this manually as part of #21300

@steveklabnik steveklabnik deleted the gh15755 branch October 25, 2017 18:28
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document integer enum discriminators in the manual
4 participants