Skip to content

Commit

Permalink
Update terminology from fieldless to unit-only
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Oct 28, 2022
1 parent 3d4745b commit 5d9300c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ reference types and `mut` or `const` in pointer types.
| Type of `e` | `U` | Cast performed by `e as U` |
|-----------------------|-----------------------|----------------------------------|
| Integer or Float type | Integer or Float type | Numeric cast |
| Field-less enum | Integer type | Enum cast |
| [Unit-only enum] | Integer type | Enum cast |
| `bool` or `char` | Integer type | Primitive to integer cast |
| `u8` | `char` | `u8` to `char` cast |
| `*T` | `*V` where `V: Sized` \* | Pointer to pointer cast |
Expand Down Expand Up @@ -643,6 +643,7 @@ See [this test] for an example of using this dependency.
[assignee expression]: ../expressions.md#place-expressions-and-value-expressions
[undefined behavior]: ../behavior-considered-undefined.md
[unit]: ../types/tuple.md
[Unit-only enum]: ../items/enumerations.md#unit-only-enum
[value expression]: ../expressions.md#place-expressions-and-value-expressions
[temporary value]: ../expressions.md#temporaries
[this test]: https://github.com/rust-lang/rust/blob/1.58.0/src/test/ui/expr/compound-assignment/eval-order.rs
Expand Down
49 changes: 31 additions & 18 deletions src/items/enumerations.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,26 @@ In this example, `Cat` is a _struct-like enum variant_, whereas `Dog` is simply
called an enum variant.

An enum where no constructors contain fields are called a
*<a id="field-less-enum">field-less enum</a>*.
*<a id="field-less-enum">field-less enum</a>*. For example, this is a fieldless enum:

```rust
enum Fieldless {
Tuple(),
Struct{},
Unit,
}
```

If a field-less enum only contains unit variants, the enum is called an
*<a id="unit-only-enum">unit-only enum</a>*. For example:

```rust
enum Enum {
Foo = 3,
Bar = 2,
Baz = 1,
}
```

## Discriminants

Expand All @@ -78,15 +97,8 @@ In two circumstances, the discriminant of a variant may be explicitly set by
following the variant name with `=` and a [constant expression]:


1. if the enumeration is fieldless; e.g.:
1. if the enumeration is "[unit-only]".

```rust
enum Enum {
Foo = 3,
Bar() = 2,
Baz {} = 1,
}
```

2. if a [primitive representation] is used. For example:

Expand Down Expand Up @@ -161,23 +173,23 @@ enum OverflowingDiscriminantError2 {

[`mem::discriminant`] returns an opaque reference to the discriminant of
an enum value which can be compared. This cannot be used to get the value
of the discriminant.
of the discriminant.

#### Casting

If an enumeration is fieldless, then its discriminant can be directly
accessed with a [numeric cast]; e.g.:
If an enumeration is [unit-only] (with no tuple and struct variants), then its
discriminant can be directly accessed with a [numeric cast]; e.g.:

```rust
enum Enum {
Unit,
Tuple(),
Struct{},
Foo,
Bar,
Baz,
}

assert_eq!(0, Enum::Unit as isize);
assert_eq!(1, Enum::Tuple() as isize);
assert_eq!(2, Enum::Struct{} as isize);
assert_eq!(0, Enum::Foo as isize);
assert_eq!(1, Enum::Bar as isize);
assert_eq!(2, Enum::Baz as isize);
```

#### Pointer Casting
Expand Down Expand Up @@ -267,6 +279,7 @@ enum E {
[enumerated type]: ../types/enum.md
[`mem::discriminant`]: ../../std/mem/fn.discriminant.html
[never type]: ../types/never.md
[unit-only]: #unit-only-enum
[numeric cast]: ../expressions/operator-expr.md#semantics
[constant expression]: ../const_eval.md#constant-expressions
[default representation]: ../type-layout.md#the-default-representation
Expand Down

0 comments on commit 5d9300c

Please sign in to comment.