@@ -196,8 +196,9 @@ Now, we have actual names, rather than positions. Good names are important,
196
196
and with a struct, we have actual names.
197
197
198
198
There _ is_ one case when a tuple struct is very useful, though, and that's a
199
- tuple struct with only one element. We call this a * newtype* , because it lets
200
- you create a new type that's similar to another one:
199
+ tuple struct with only one element. We call this the * newtype* pattern, because
200
+ it allows you to create a new type, distinct from that of its contained value
201
+ and expressing its own semantic meaning:
201
202
202
203
``` {rust}
203
204
struct Inches(i32);
@@ -216,7 +217,7 @@ destructuring `let`, as we discussed previously in 'tuples.' In this case, the
216
217
217
218
Finally, Rust has a "sum type", an * enum* . Enums are an incredibly useful
218
219
feature of Rust, and are used throughout the standard library. An ` enum ` is
219
- a type which ties a set of alternates to a specific name. For example, below
220
+ a type which relates a set of alternates to a specific name. For example, below
220
221
we define ` Character ` to be either a ` Digit ` or something else. These
221
222
can be used via their fully scoped names: ` Character::Other ` (more about ` :: `
222
223
below).
@@ -228,8 +229,8 @@ enum Character {
228
229
}
229
230
```
230
231
231
- An ` enum ` variant can be defined as most normal types. Below are some example
232
- types which also would be allowed in an ` enum ` .
232
+ Most normal types are allowed as the variant components of an ` enum ` . Here are
233
+ some examples:
233
234
234
235
``` rust
235
236
struct Empty ;
@@ -239,15 +240,15 @@ struct Status { Health: i32, Mana: i32, Attack: i32, Defense: i32 }
239
240
struct HeightDatabase (Vec <i32 >);
240
241
```
241
242
242
- So you see that depending on the sub-datastructure, the ` enum ` variant, same as
243
- a struct, may or may not hold data. That is, in ` Character ` , ` Digit ` is a name
244
- tied to an ` i32 ` where ` Other ` is just a name. However, the fact that they are
245
- distinct makes this very useful.
243
+ You see that, depending on its type, an ` enum ` variant may or may not hold data.
244
+ In ` Character ` , for instance, ` Digit ` gives a meaningful name for an ` i32 `
245
+ value, where ` Other ` is only a name. However, the fact that they represent
246
+ distinct categories of ` Character ` is a very useful property .
246
247
247
- As with structures, enums don't by default have access to operators such as
248
- compare ( ` == ` and ` != ` ), binary operations ( ` * ` and ` + ` ), and order
249
- ( ` < ` and ` >= ` ) . As such, using the previous ` Character ` type, the
250
- following code is invalid:
248
+ As with structures, the variants of an enum by default are not comparable with
249
+ equality operators ( ` == ` , ` != ` ), have no ordering ( ` < ` , ` >= ` , etc. ), and do not
250
+ support other binary operations such as ` * ` and ` + ` . As such, the following code
251
+ is invalid for the example ` Character ` type :
251
252
252
253
``` {rust,ignore}
253
254
// These assignments both succeed
@@ -265,9 +266,10 @@ let four_equals_ten = four == ten;
265
266
```
266
267
267
268
This may seem rather limiting, but it's a limitation which we can overcome.
268
- There are two ways: by implementing equality ourselves, or by using the
269
- [ ` match ` ] [ match ] keyword. We don't know enough about Rust to implement equality
270
- yet, but we can use the ` Ordering ` enum from the standard library, which does:
269
+ There are two ways: by implementing equality ourselves, or by pattern matching
270
+ variants with [ ` match ` ] [ match ] expressions, which you'll learn in the next
271
+ chapter. We don't know enough about Rust to implement equality yet, but we can
272
+ use the ` Ordering ` enum from the standard library, which does:
271
273
272
274
```
273
275
enum Ordering {
@@ -277,9 +279,8 @@ enum Ordering {
277
279
}
278
280
```
279
281
280
- Because we did not define ` Ordering ` , we must import it (from the std
281
- library) with the ` use ` keyword. Here's an example of how ` Ordering ` is
282
- used:
282
+ Because ` Ordering ` has already been defined for us, we will import it with the
283
+ ` use ` keyword. Here's an example of how it is used:
283
284
284
285
``` {rust}
285
286
use std::cmp::Ordering;
@@ -313,17 +314,17 @@ the standard library if you need them.
313
314
314
315
Okay, let's talk about the actual code in the example. ` cmp ` is a function that
315
316
compares two things, and returns an ` Ordering ` . We return either
316
- ` Ordering::Less ` , ` Ordering::Greater ` , or ` Ordering::Equal ` , depending on if
317
- the two values are less, greater, or equal. Note that each variant of the
318
- ` enum ` is namespaced under the ` enum ` itself: it's ` Ordering::Greater ` not
319
- ` Greater ` .
317
+ ` Ordering::Less ` , ` Ordering::Greater ` , or ` Ordering::Equal ` , depending on
318
+ whether the first value is less than , greater than , or equal to the second. Note
319
+ that each variant of the ` enum ` is namespaced under the ` enum ` itself: it's
320
+ ` Ordering::Greater ` , not ` Greater ` .
320
321
321
322
The ` ordering ` variable has the type ` Ordering ` , and so contains one of the
322
323
three values. We then do a bunch of ` if ` /` else ` comparisons to check which
323
324
one it is.
324
325
325
- This ` Ordering::Greater ` notation is too long. Let's use ` use ` to import the
326
- ` enum ` variants instead. This will avoid full scoping:
326
+ This ` Ordering::Greater ` notation is too long. Let's use another form of ` use `
327
+ to import the ` enum ` variants instead. This will avoid full scoping:
327
328
328
329
``` {rust}
329
330
use std::cmp::Ordering::{self, Equal, Less, Greater};
@@ -347,14 +348,15 @@ fn main() {
347
348
```
348
349
349
350
Importing variants is convenient and compact, but can also cause name conflicts,
350
- so do this with caution. It's considered good style to rarely import variants
351
- for this reason.
352
-
353
- As you can see, ` enum ` s are quite a powerful tool for data representation, and are
354
- even more useful when they're [ generic] [ generics ] across types. Before we
355
- get to generics, though, let's talk about how to use them with pattern matching, a
356
- tool that will let us deconstruct this sum type (the type theory term for enums)
357
- in a very elegant way and avoid all these messy ` if ` /` else ` s.
351
+ so do this with caution. For this reason, it's normally considered better style
352
+ to ` use ` an enum rather than its variants directly.
353
+
354
+ As you can see, ` enum ` s are quite a powerful tool for data representation, and
355
+ are even more useful when they're [ generic] [ generics ] across types. Before we
356
+ get to generics, though, let's talk about how to use enums with pattern
357
+ matching, a tool that will let us deconstruct sum types (the type theory term
358
+ for enums) like ` Ordering ` in a very elegant way that avoids all these messy
359
+ and brittle ` if ` /` else ` s.
358
360
359
361
360
362
[ arity ] : ./glossary.html#arity
0 commit comments