@@ -59,7 +59,26 @@ In this example, `Cat` is a _struct-like enum variant_, whereas `Dog` is simply
5959called an enum variant.
6060
6161An 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+ ```
6382
6483## Discriminants
6584
@@ -78,15 +97,8 @@ In two circumstances, the discriminant of a variant may be explicitly set by
7897following the variant name with ` = ` and a [ constant expression] :
7998
8099
81- 1 . if the enumeration is fieldless; e.g.:
100+ 1 . if the enumeration is " [ unit-only ] ".
82101
83- ``` rust
84- enum Enum {
85- Foo = 3 ,
86- Bar () = 2 ,
87- Baz {} = 1 ,
88- }
89- ```
90102
911032 . if a [ primitive representation] is used. For example:
92104
@@ -161,23 +173,23 @@ enum OverflowingDiscriminantError2 {
161173
162174[ ` mem::discriminant ` ] returns an opaque reference to the discriminant of
163175an enum value which can be compared. This cannot be used to get the value
164- of the discriminant.
176+ of the discriminant.
165177
166178#### Casting
167179
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.:
170182
171183``` rust
172184enum Enum {
173- Unit ,
174- Tuple () ,
175- Struct {} ,
185+ Foo ,
186+ Bar ,
187+ Baz ,
176188}
177189
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 );
181193```
182194
183195#### Pointer Casting
@@ -267,6 +279,7 @@ enum E {
267279[ enumerated type ] : ../types/enum.md
268280[ `mem::discriminant` ] : ../../std/mem/fn.discriminant.html
269281[ never type ] : ../types/never.md
282+ [ unit-only ] : #unit-only-enum
270283[ numeric cast ] : ../expressions/operator-expr.md#semantics
271284[ constant expression ] : ../const_eval.md#constant-expressions
272285[ default representation ] : ../type-layout.md#the-default-representation
0 commit comments