Skip to content

[r+] Add enum discriminants to the reference. - #21047

Closed
steveklabnik wants to merge 1 commit into
rust-lang:masterfrom
steveklabnik:gh15755
Closed

[r+] Add enum discriminants to the reference.#21047
steveklabnik wants to merge 1 commit into
rust-lang:masterfrom
steveklabnik:gh15755

Conversation

@steveklabnik

Copy link
Copy Markdown
Contributor

Fixes#15755

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

Comment threadsrc/doc/reference.md Outdated

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

@sfackler addressed

Comment threadsrc/doc/reference.md

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
Contributor

r+ with @sfackler 's nit addressed

@nikomatsakisnikomatsakis changed the title Add enum discriminats to the reference.r+] Add enum discriminants to the reference.Jan 14, 2015
@nikomatsakisnikomatsakis 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
Copy Markdown
ContributorAuthor

Doing this manually as part of #21300

@steveklabnik
steveklabnik deleted the gh15755 branch October 25, 2017 18:28
lnicola pushed a commit to lnicola/rust that referenced this pull request Nov 19, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in 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

@steveklabnik@rust-highfive@nikomatsakis@sfackler