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
@@ -62,12 +62,11 @@ learn in the next section. We don’t know enough about Rust to implement
62
62
equality yet, but we’ll find out in the [ ` traits ` ] [ traits ] section.
63
63
64
64
[ match ] : match.html
65
- [ if-let ] : if-let.html
66
65
[ traits ] : traits.html
67
66
68
67
# Constructors as functions
69
68
70
- An enum’s constructors can also be used like functions . For example:
69
+ An ` enum ` constructor can also be used like a function . For example:
71
70
72
71
``` rust
73
72
# enum Message {
@@ -76,7 +75,7 @@ An enum’s constructors can also be used like functions. For example:
76
75
let m = Message :: Write (" Hello, world" . to_string ());
77
76
```
78
77
79
- Is the same as
78
+ is the same as
80
79
81
80
``` rust
82
81
# enum Message {
0 commit comments