diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-01-12 16:07:44 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-01-17 10:49:49 -0500 |
| commit | a2e277edf49cb05e79c0346de9a145e4ce6754cb (patch) | |
| tree | 39d21a060a60b0ee011a40712e33ef5886ee9cf2 /src/doc/reference.md | |
| parent | 868669f420df66c6acd866391a855200efa4a5d6 (diff) | |
| download | rust-a2e277edf49cb05e79c0346de9a145e4ce6754cb.tar.gz rust-a2e277edf49cb05e79c0346de9a145e4ce6754cb.zip | |
Add enum discriminats to the reference.
Fixes #15755
Diffstat (limited to 'src/doc/reference.md')
| -rw-r--r-- | src/doc/reference.md | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index c8e31f27b35..a27d6c6e268 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1413,6 +1413,27 @@ 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 have a discriminant. You can assign them explicitly: + +``` +enum Foo { + Bar = 123, +} +``` + +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: + +``` +# enum Foo { Bar = 123 } +let x = Foo::Bar as u32; // x is now 123u32 +``` + +This only works as long as none of the variants have data attached. If +it were `Bar(i32)`, this is disallowed. + ### Constant items ```{.ebnf .gram} |
