1
1
% Enums
2
2
3
- An ` enum ` in Rust is a type that represents data that could be one of
4
- several possible variants:
3
+ An ` enum ` in Rust is a type that represents data that is one of
4
+ several possible variants. Each variant in the ` enum ` can optionally
5
+ have data associated with it:
5
6
6
7
``` rust
7
8
enum Message {
@@ -12,9 +13,8 @@ enum Message {
12
13
}
13
14
```
14
15
15
- Each variant can optionally have data associated with it. The syntax for
16
- defining variants resembles the syntaxes used to define structs: you can
17
- have variants with no data (like unit-like structs), variants with named
16
+ The syntax for defining variants resembles the syntaxes used to define structs:
17
+ you can have variants with no data (like unit-like structs), variants with named
18
18
data, and variants with unnamed data (like tuple structs). Unlike
19
19
separate struct definitions, however, an ` enum ` is a single type. A
20
20
value of the enum can match any of the variants. For this reason, an
@@ -41,7 +41,7 @@ let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 };
41
41
Both variants are named ` Move ` , but since they’re scoped to the name of
42
42
the enum, they can both be used without conflict.
43
43
44
- A value of an enum type contains information about which variant it is,
44
+ A value of an ` enum ` type contains information about which variant it is,
45
45
in addition to any data associated with that variant. This is sometimes
46
46
referred to as a ‘tagged union’, since the data includes a ‘tag’
47
47
indicating what type it is. The compiler uses this information to
@@ -67,7 +67,7 @@ equality yet, but we’ll find out in the [`traits`][traits] section.
67
67
68
68
# Constructors as functions
69
69
70
- An enum’s constructors can also be used like functions . For example:
70
+ An ` enum ` constructor can also be used like a function . For example:
71
71
72
72
``` rust
73
73
# enum Message {
@@ -76,7 +76,7 @@ An enum’s constructors can also be used like functions. For example:
76
76
let m = Message :: Write (" Hello, world" . to_string ());
77
77
```
78
78
79
- Is the same as
79
+ is the same as
80
80
81
81
``` rust
82
82
# enum Message {
0 commit comments