Add enum discriminats to the reference.

Fixes #15755
This commit is contained in:
Steve Klabnik 2015-01-12 16:07:44 -05:00
parent 868669f420
commit a2e277edf4

View File

@ -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}