@@ -1100,10 +1100,17 @@ enum Ordering {
1100
1100
```
1101
1101
1102
1102
An ` Ordering ` can only be _ one_ of ` Less ` , ` Equal ` , or ` Greater ` at any given
1103
- time. Here's an example:
1103
+ time.
1104
+
1105
+ Because ` Ordering ` is provided by the standard library, we can use the ` use `
1106
+ keyword to use it in our code. We'll learn more about ` use ` later, but it's
1107
+ used to bring names into scope.
1108
+
1109
+ Here's an example of how to use ` Ordering ` :
1104
1110
1105
1111
``` {rust}
1106
- # use std::cmp::Ordering;
1112
+ use std::cmp::Ordering;
1113
+
1107
1114
fn cmp(a: int, b: int) -> Ordering {
1108
1115
if a < b { Ordering::Less }
1109
1116
else if a > b { Ordering::Greater }
@@ -1126,18 +1133,25 @@ fn main() {
1126
1133
}
1127
1134
```
1128
1135
1129
- ` cmp ` is a function that compares two things, and returns an ` Ordering ` . We
1130
- return either ` Less ` , ` Greater ` , or ` Equal ` , depending on if the two values
1131
- are greater, less, or equal.
1136
+ There's a symbol here we haven't seen before: the double colon (` :: ` ).
1137
+ This is used to indicate a namesapce. In this case, ` Ordering ` lives in
1138
+ the ` cmp ` submodule of the ` std ` module. We'll talk more about modules
1139
+ later in the guide. For now, all you need to know is that you can ` use `
1140
+ things from the standard library if you need them.
1132
1141
1133
- The ` ordering ` variable has the type ` Ordering ` , and so contains one of the
1134
- three values. We can then do a bunch of ` if ` /` else ` comparisons to check
1135
- which one it is.
1142
+ Okay, let's talk about the actual code in the example. ` cmp ` is a function that
1143
+ compares two things, and returns an ` Ordering ` . We return either
1144
+ ` Ordering::Less ` , ` Ordering::Greater ` , or ` Ordering::Equal ` , depending on if
1145
+ the two values are greater, less, or equal. Note that each variant of the
1146
+ ` enum ` is namespaced under the ` enum ` itself: it's ` Ordering::Greater ` not
1147
+ ` Greater ` .
1136
1148
1137
- However, repeated ` if ` /` else ` comparisons get quite tedious. Rust has a feature
1138
- that not only makes them nicer to read, but also makes sure that you never
1139
- miss a case. Before we get to that, though, let's talk about another kind of
1140
- enum: one with values.
1149
+ The ` ordering ` variable has the type ` Ordering ` , and so contains one of the
1150
+ three values. We can then do a bunch of ` if ` /` else ` comparisons to check which
1151
+ one it is. However, repeated ` if ` /` else ` comparisons get quite tedious. Rust
1152
+ has a feature that not only makes them nicer to read, but also makes sure that
1153
+ you never miss a case. Before we get to that, though, let's talk about another
1154
+ kind of enum: one with values.
1141
1155
1142
1156
This enum has two variants, one of which has a value:
1143
1157
@@ -1170,18 +1184,19 @@ enum StringResult {
1170
1184
ErrorReason(String),
1171
1185
}
1172
1186
```
1173
- Where a ` StringResult ` is either a ` StringOK ` , with the result of a computation, or an
1174
- ` ErrorReason ` with a ` String ` explaining what caused the computation to fail. These kinds of
1175
- ` enum ` s are actually very useful and are even part of the standard library.
1187
+ Where a ` StringResult ` is either a ` StringResult::StringOK ` , with the result of
1188
+ a computation, or an ` StringResult::ErrorReason ` with a ` String ` explaining
1189
+ what caused the computation to fail. These kinds of ` enum ` s are actually very
1190
+ useful and are even part of the standard library.
1176
1191
1177
- Enum variants are namespaced under the enum names. For example, here is an example of using
1178
- our ` StringResult ` :
1192
+ Here is an example of using our ` StringResult ` :
1179
1193
1180
1194
``` rust
1181
- # enum StringResult {
1182
- # StringOK (String ),
1183
- # ErrorReason (String ),
1184
- # }
1195
+ enum StringResult {
1196
+ StringOK (String ),
1197
+ ErrorReason (String ),
1198
+ }
1199
+
1185
1200
fn respond (greeting : & str ) -> StringResult {
1186
1201
if greeting == " Hello" {
1187
1202
StringResult :: StringOK (" Good morning!" . to_string ())
@@ -1191,10 +1206,7 @@ fn respond(greeting: &str) -> StringResult {
1191
1206
}
1192
1207
```
1193
1208
1194
- Notice that we need both the enum name and the variant name: ` StringResult::StringOK ` , but
1195
- we didn't need to with ` Ordering ` – we just said ` Greater ` rather than ` Ordering::Greater ` .
1196
- There's a reason: the Rust prelude imports the variants of ` Ordering ` as well as the enum
1197
- itself. We can use the ` use ` keyword to do something similar with ` StringResult ` :
1209
+ That's a lot of typing! We can use the ` use ` keyword to make it shorter:
1198
1210
1199
1211
``` rust
1200
1212
use StringResult :: StringOK ;
@@ -1216,12 +1228,11 @@ fn respond(greeting: &str) -> StringResult {
1216
1228
}
1217
1229
```
1218
1230
1219
- We'll learn more about ` use ` later, but it's used to bring names into scope. ` use ` declarations
1220
- must come before anything else, which looks a little strange in this example, since we ` use `
1221
- the variants before we define them. Anyway, in the body of ` respond ` , we can just say ` StringOK `
1222
- now, rather than the full ` StringResult::StringOK ` . Importing variants can be convenient, but can
1223
- also cause name conflicts, so do this with caution. It's considered good style to rarely import
1224
- variants for this reason.
1231
+ ` use ` declarations must come before anything else, which looks a little strange in this example,
1232
+ since we ` use ` the variants before we define them. Anyway, in the body of ` respond ` , we can just
1233
+ say ` StringOK ` now, rather than the full ` StringResult::StringOK ` . Importing variants can be
1234
+ convenient, but can also cause name conflicts, so do this with caution. It's considered good style
1235
+ to rarely import variants for this reason.
1225
1236
1226
1237
As you can see, ` enum ` s with values are quite a powerful tool for data representation,
1227
1238
and can be even more useful when they're generic across types. Before we get to generics,
@@ -1275,7 +1286,8 @@ for every possible value of `x`, and so our program will compile successfully.
1275
1286
section on enums?
1276
1287
1277
1288
``` {rust}
1278
- # use std::cmp::Ordering;
1289
+ use std::cmp::Ordering;
1290
+
1279
1291
fn cmp(a: int, b: int) -> Ordering {
1280
1292
if a < b { Ordering::Less }
1281
1293
else if a > b { Ordering::Greater }
@@ -1301,7 +1313,8 @@ fn main() {
1301
1313
We can re-write this as a ` match ` :
1302
1314
1303
1315
``` {rust}
1304
- # use std::cmp::Ordering;
1316
+ use std::cmp::Ordering;
1317
+
1305
1318
fn cmp(a: int, b: int) -> Ordering {
1306
1319
if a < b { Ordering::Less }
1307
1320
else if a > b { Ordering::Greater }
@@ -1362,7 +1375,8 @@ side of a `let` binding or directly where an expression is used. We could
1362
1375
also implement the previous line like this:
1363
1376
1364
1377
``` {rust}
1365
- # use std::cmp::Ordering;
1378
+ use std::cmp::Ordering;
1379
+
1366
1380
fn cmp(a: int, b: int) -> Ordering {
1367
1381
if a < b { Ordering::Less }
1368
1382
else if a > b { Ordering::Greater }
0 commit comments