parent
868669f420
commit
a2e277edf4
1 changed files with 21 additions and 0 deletions
|
@ -1413,6 +1413,27 @@ a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
|
||||||
In this example, `Cat` is a _struct-like enum variant_,
|
In this example, `Cat` is a _struct-like enum variant_,
|
||||||
whereas `Dog` is simply called an 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
|
### Constant items
|
||||||
|
|
||||||
```{.ebnf .gram}
|
```{.ebnf .gram}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue