@@ -59,7 +59,26 @@ In this example, `Cat` is a _struct-like enum variant_, whereas `Dog` is simply
59
59
called an enum variant.
60
60
61
61
An enum where no constructors contain fields are called a
62
- * <a id =" field-less-enum " >field-less enum</a >* .
62
+ * <a id =" field-less-enum " >field-less enum</a >* . For example, this is a fieldless enum:
63
+
64
+ ``` rust
65
+ enum Fieldless {
66
+ Tuple (),
67
+ Struct {},
68
+ Unit ,
69
+ }
70
+ ```
71
+
72
+ If a field-less enum only contains unit variants, the enum is called an
73
+ * <a id =" unit-only-enum " >unit-only enum</a >* . For example:
74
+
75
+ ``` rust
76
+ enum Enum {
77
+ Foo = 3 ,
78
+ Bar = 2 ,
79
+ Baz = 1 ,
80
+ }
81
+ ```
63
82
64
83
## Discriminants
65
84
@@ -78,15 +97,8 @@ In two circumstances, the discriminant of a variant may be explicitly set by
78
97
following the variant name with ` = ` and a [ constant expression] :
79
98
80
99
81
- 1 . if the enumeration is fieldless; e.g.:
100
+ 1 . if the enumeration is " [ unit-only ] ".
82
101
83
- ``` rust
84
- enum Enum {
85
- Foo = 3 ,
86
- Bar () = 2 ,
87
- Baz {} = 1 ,
88
- }
89
- ```
90
102
91
103
2 . if a [ primitive representation] is used. For example:
92
104
@@ -161,23 +173,23 @@ enum OverflowingDiscriminantError2 {
161
173
162
174
[ ` mem::discriminant ` ] returns an opaque reference to the discriminant of
163
175
an enum value which can be compared. This cannot be used to get the value
164
- of the discriminant.
176
+ of the discriminant.
165
177
166
178
#### Casting
167
179
168
- If an enumeration is fieldless, then its discriminant can be directly
169
- accessed with a [ numeric cast] ; e.g.:
180
+ If an enumeration is [ unit-only ] (with no tuple and struct variants), then its
181
+ discriminant can be directly accessed with a [ numeric cast] ; e.g.:
170
182
171
183
``` rust
172
184
enum Enum {
173
- Unit ,
174
- Tuple () ,
175
- Struct {} ,
185
+ Foo ,
186
+ Bar ,
187
+ Baz ,
176
188
}
177
189
178
- assert_eq! (0 , Enum :: Unit as isize );
179
- assert_eq! (1 , Enum :: Tuple () as isize );
180
- assert_eq! (2 , Enum :: Struct {} as isize );
190
+ assert_eq! (0 , Enum :: Foo as isize );
191
+ assert_eq! (1 , Enum :: Bar as isize );
192
+ assert_eq! (2 , Enum :: Baz as isize );
181
193
```
182
194
183
195
#### Pointer Casting
@@ -267,6 +279,7 @@ enum E {
267
279
[ enumerated type ] : ../types/enum.md
268
280
[ `mem::discriminant` ] : ../../std/mem/fn.discriminant.html
269
281
[ never type ] : ../types/never.md
282
+ [ unit-only ] : #unit-only-enum
270
283
[ numeric cast ] : ../expressions/operator-expr.md#semantics
271
284
[ constant expression ] : ../const_eval.md#constant-expressions
272
285
[ default representation ] : ../type-layout.md#the-default-representation
0 commit comments