@@ -58,20 +58,18 @@ a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
58
58
In this example, ` Cat ` is a _ struct-like enum variant_ , whereas ` Dog ` is simply
59
59
called an enum variant.
60
60
61
+ An enum where no constructors contain fields are called a
62
+ * <a name =" field-less-enum " >field-less enum</a >* .
63
+
61
64
## Discriminants
62
65
63
66
Each enum instance has a _ discriminant_ : an integer logically associated to it
64
- that is used to determine which variant it holds. An opaque reference to this
65
- discriminant can be obtained with the [ ` mem::discriminant ` ] function.
67
+ that is used to determine which variant it holds.
66
68
67
69
Under the [ default representation] , the discriminant is interpreted as
68
70
an ` isize ` value. However, the compiler is allowed to use a smaller type (or
69
71
another means of distinguishing variants) in its actual memory layout.
70
72
71
- If the [ primitive representation] or the [ ` C ` representation] is used, the
72
- leading bytes of a variant (for example, two bytes if ` #[repr(u16)] ` is used), will
73
- correspond exactly to the discriminant.
74
-
75
73
### Assigning Discriminant Values
76
74
77
75
#### Explicit Discriminants
@@ -85,7 +83,6 @@ following the variant name with `=` and a [constant expression]:
85
83
if the enumeration is "C-like" (i.e., it has no tuple or struct variants); e.g.:
86
84
87
85
``` rust
88
- # #![feature(arbitrary_enum_discriminant)]
89
86
enum Enum {
90
87
Foo = 3 ,
91
88
Bar = 2 ,
@@ -98,7 +95,6 @@ enum Enum {
98
95
if a [ primitive representation] is used. For example:
99
96
100
97
``` rust
101
- # #![feature(arbitrary_enum_discriminant)]
102
98
#[repr(u8 )]
103
99
enum Enum {
104
100
Unit = 3 ,
@@ -165,12 +161,18 @@ enum OverflowingDiscriminantError2 {
165
161
}
166
162
```
167
163
168
- ### Accessing Discriminant Values
164
+ ### Accessing Discriminant
165
+
166
+ #### Via ` mem::discriminant `
167
+
168
+ [ ` mem::discriminant ` ] returns an opaque reference to the discriminant of
169
+ an enum value which can be compared. This cannot be used to get the value
170
+ of the discriminant.
169
171
170
172
#### Casting
171
173
172
- If there is no data attached to * any * of the variants of an enumeration, then
173
- the discriminant can be directly accessed with a [ numeric cast] ; e.g.:
174
+ If an enumeration is fieldless, then its discriminant can be directly
175
+ accessed with a [ numeric cast] ; e.g.:
174
176
175
177
``` rust
176
178
enum Enum {
0 commit comments