@@ -1106,10 +1106,17 @@ enum Ordering {
1106
1106
```
1107
1107
1108
1108
An ` Ordering ` can only be _ one_ of ` Less ` , ` Equal ` , or ` Greater ` at any given
1109
- time. Here's an example:
1109
+ time.
1110
+
1111
+ Because ` Ordering ` is provided by the standard library, we can use the ` use `
1112
+ keyword to use it in our code. We'll learn more about ` use ` later, but it's
1113
+ used to bring names into scope.
1114
+
1115
+ Here's an example of how to use ` Ordering ` :
1110
1116
1111
1117
``` {rust}
1112
- # use std::cmp::Ordering;
1118
+ use std::cmp::Ordering;
1119
+
1113
1120
fn cmp(a: int, b: int) -> Ordering {
1114
1121
if a < b { Ordering::Less }
1115
1122
else if a > b { Ordering::Greater }
@@ -1132,18 +1139,25 @@ fn main() {
1132
1139
}
1133
1140
```
1134
1141
1135
- ` cmp ` is a function that compares two things, and returns an ` Ordering ` . We
1136
- return either ` Less ` , ` Greater ` , or ` Equal ` , depending on if the two values
1137
- are greater, less, or equal.
1142
+ There's a symbol here we haven't seen before: the double colon (` :: ` ).
1143
+ This is used to indicate a namesapce. In this case, ` Ordering ` lives in
1144
+ the ` cmp ` submodule of the ` std ` module. We'll talk more about modules
1145
+ later in the guide. For now, all you need to know is that you can ` use `
1146
+ things from the standard library if you need them.
1138
1147
1139
- The ` ordering ` variable has the type ` Ordering ` , and so contains one of the
1140
- three values. We can then do a bunch of ` if ` /` else ` comparisons to check
1141
- which one it is.
1148
+ Okay, let's talk about the actual code in the example. ` cmp ` is a function that
1149
+ compares two things, and returns an ` Ordering ` . We return either
1150
+ ` Ordering::Less ` , ` Ordering::Greater ` , or ` Ordering::Equal ` , depending on if
1151
+ the two values are greater, less, or equal. Note that each variant of the
1152
+ ` enum ` is namespaced under the ` enum ` itself: it's ` Ordering::Greater ` not
1153
+ ` Greater ` .
1142
1154
1143
- However, repeated ` if ` /` else ` comparisons get quite tedious. Rust has a feature
1144
- that not only makes them nicer to read, but also makes sure that you never
1145
- miss a case. Before we get to that, though, let's talk about another kind of
1146
- enum: one with values.
1155
+ The ` ordering ` variable has the type ` Ordering ` , and so contains one of the
1156
+ three values. We can then do a bunch of ` if ` /` else ` comparisons to check which
1157
+ one it is. However, repeated ` if ` /` else ` comparisons get quite tedious. Rust
1158
+ has a feature that not only makes them nicer to read, but also makes sure that
1159
+ you never miss a case. Before we get to that, though, let's talk about another
1160
+ kind of enum: one with values.
1147
1161
1148
1162
This enum has two variants, one of which has a value:
1149
1163
@@ -1176,18 +1190,19 @@ enum StringResult {
1176
1190
ErrorReason(String),
1177
1191
}
1178
1192
```
1179
- Where a ` StringResult ` is either a ` StringOK ` , with the result of a computation, or an
1180
- ` ErrorReason ` with a ` String ` explaining what caused the computation to fail. These kinds of
1181
- ` enum ` s are actually very useful and are even part of the standard library.
1193
+ Where a ` StringResult ` is either a ` StringResult::StringOK ` , with the result of
1194
+ a computation, or an ` StringResult::ErrorReason ` with a ` String ` explaining
1195
+ what caused the computation to fail. These kinds of ` enum ` s are actually very
1196
+ useful and are even part of the standard library.
1182
1197
1183
- Enum variants are namespaced under the enum names. For example, here is an example of using
1184
- our ` StringResult ` :
1198
+ Here is an example of using our ` StringResult ` :
1185
1199
1186
1200
``` rust
1187
- # enum StringResult {
1188
- # StringOK (String ),
1189
- # ErrorReason (String ),
1190
- # }
1201
+ enum StringResult {
1202
+ StringOK (String ),
1203
+ ErrorReason (String ),
1204
+ }
1205
+
1191
1206
fn respond (greeting : & str ) -> StringResult {
1192
1207
if greeting == " Hello" {
1193
1208
StringResult :: StringOK (" Good morning!" . to_string ())
@@ -1197,10 +1212,7 @@ fn respond(greeting: &str) -> StringResult {
1197
1212
}
1198
1213
```
1199
1214
1200
- Notice that we need both the enum name and the variant name: ` StringResult::StringOK ` , but
1201
- we didn't need to with ` Ordering ` – we just said ` Greater ` rather than ` Ordering::Greater ` .
1202
- There's a reason: the Rust prelude imports the variants of ` Ordering ` as well as the enum
1203
- itself. We can use the ` use ` keyword to do something similar with ` StringResult ` :
1215
+ That's a lot of typing! We can use the ` use ` keyword to make it shorter:
1204
1216
1205
1217
``` rust
1206
1218
use StringResult :: StringOK ;
@@ -1222,12 +1234,11 @@ fn respond(greeting: &str) -> StringResult {
1222
1234
}
1223
1235
```
1224
1236
1225
- We'll learn more about ` use ` later, but it's used to bring names into scope. ` use ` declarations
1226
- must come before anything else, which looks a little strange in this example, since we ` use `
1227
- the variants before we define them. Anyway, in the body of ` respond ` , we can just say ` StringOK `
1228
- now, rather than the full ` StringResult::StringOK ` . Importing variants can be convenient, but can
1229
- also cause name conflicts, so do this with caution. It's considered good style to rarely import
1230
- variants for this reason.
1237
+ ` use ` declarations must come before anything else, which looks a little strange in this example,
1238
+ since we ` use ` the variants before we define them. Anyway, in the body of ` respond ` , we can just
1239
+ say ` StringOK ` now, rather than the full ` StringResult::StringOK ` . Importing variants can be
1240
+ convenient, but can also cause name conflicts, so do this with caution. It's considered good style
1241
+ to rarely import variants for this reason.
1231
1242
1232
1243
As you can see, ` enum ` s with values are quite a powerful tool for data representation,
1233
1244
and can be even more useful when they're generic across types. Before we get to generics,
@@ -1281,7 +1292,8 @@ for every possible value of `x`, and so our program will compile successfully.
1281
1292
section on enums?
1282
1293
1283
1294
``` {rust}
1284
- # use std::cmp::Ordering;
1295
+ use std::cmp::Ordering;
1296
+
1285
1297
fn cmp(a: int, b: int) -> Ordering {
1286
1298
if a < b { Ordering::Less }
1287
1299
else if a > b { Ordering::Greater }
@@ -1307,7 +1319,8 @@ fn main() {
1307
1319
We can re-write this as a ` match ` :
1308
1320
1309
1321
``` {rust}
1310
- # use std::cmp::Ordering;
1322
+ use std::cmp::Ordering;
1323
+
1311
1324
fn cmp(a: int, b: int) -> Ordering {
1312
1325
if a < b { Ordering::Less }
1313
1326
else if a > b { Ordering::Greater }
@@ -1368,7 +1381,8 @@ side of a `let` binding or directly where an expression is used. We could
1368
1381
also implement the previous line like this:
1369
1382
1370
1383
``` {rust}
1371
- # use std::cmp::Ordering;
1384
+ use std::cmp::Ordering;
1385
+
1372
1386
fn cmp(a: int, b: int) -> Ordering {
1373
1387
if a < b { Ordering::Less }
1374
1388
else if a > b { Ordering::Greater }
0 commit comments