Skip to content

Commit cabce65

Browse files
committed
Applied suggestions from Havvy
1 parent 811232f commit cabce65

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/items/enumerations.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,18 @@ a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
5858
In this example, `Cat` is a _struct-like enum variant_, whereas `Dog` is simply
5959
called an enum variant.
6060

61+
An enum where no constructors contain fields are called a
62+
*<a name="field-less-enum">field-less enum</a>*.
63+
6164
## Discriminants
6265

6366
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.
6668

6769
Under the [default representation], the discriminant is interpreted as
6870
an `isize` value. However, the compiler is allowed to use a smaller type (or
6971
another means of distinguishing variants) in its actual memory layout.
7072

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-
7573
### Assigning Discriminant Values
7674

7775
#### Explicit Discriminants
@@ -85,7 +83,6 @@ following the variant name with `=` and a [constant expression]:
8583
if the enumeration is "C-like" (i.e., it has no tuple or struct variants); e.g.:
8684

8785
```rust
88-
# #![feature(arbitrary_enum_discriminant)]
8986
enum Enum {
9087
Foo = 3,
9188
Bar = 2,
@@ -98,7 +95,6 @@ enum Enum {
9895
if a [primitive representation] is used. For example:
9996

10097
```rust
101-
# #![feature(arbitrary_enum_discriminant)]
10298
#[repr(u8)]
10399
enum Enum {
104100
Unit = 3,
@@ -165,12 +161,18 @@ enum OverflowingDiscriminantError2 {
165161
}
166162
```
167163

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.
169171

170172
#### Casting
171173

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.:
174176

175177
```rust
176178
enum Enum {

src/type-layout.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ used with any other representation.
558558
[`Sized`]: ../std/marker/trait.Sized.html
559559
[`Copy`]: ../std/marker/trait.Copy.html
560560
[dynamically sized types]: dynamically-sized-types.md
561-
[field-less enums]: items/enumerations.md#custom-discriminant-values-for-fieldless-enumerations
561+
[field-less enums]: items/enumerations.md#field-less-enum
562562
[enumerations]: items/enumerations.md
563563
[zero-variant enums]: items/enumerations.md#zero-variant-enums
564564
[undefined behavior]: behavior-considered-undefined.md

0 commit comments

Comments
 (0)