@@ -1106,10 +1106,17 @@ enum Ordering {
11061106```
11071107
11081108An ` 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 ` :
11101116
11111117``` {rust}
1112- # use std::cmp::Ordering;
1118+ use std::cmp::Ordering;
1119+
11131120fn cmp(a: int, b: int) -> Ordering {
11141121 if a < b { Ordering::Less }
11151122 else if a > b { Ordering::Greater }
@@ -1132,18 +1139,25 @@ fn main() {
11321139}
11331140```
11341141
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.
11381147
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 ` .
11421154
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.
11471161
11481162This enum has two variants, one of which has a value:
11491163
@@ -1176,18 +1190,19 @@ enum StringResult {
11761190 ErrorReason(String),
11771191}
11781192```
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.
11821197
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 ` :
11851199
11861200``` rust
1187- # enum StringResult {
1188- # StringOK (String ),
1189- # ErrorReason (String ),
1190- # }
1201+ enum StringResult {
1202+ StringOK (String ),
1203+ ErrorReason (String ),
1204+ }
1205+
11911206fn respond (greeting : & str ) -> StringResult {
11921207 if greeting == " Hello" {
11931208 StringResult :: StringOK (" Good morning!" . to_string ())
@@ -1197,10 +1212,7 @@ fn respond(greeting: &str) -> StringResult {
11971212}
11981213```
11991214
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:
12041216
12051217``` rust
12061218use StringResult :: StringOK ;
@@ -1222,12 +1234,11 @@ fn respond(greeting: &str) -> StringResult {
12221234}
12231235```
12241236
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.
12311242
12321243As you can see, ` enum ` s with values are quite a powerful tool for data representation,
12331244and 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.
12811292section on enums?
12821293
12831294``` {rust}
1284- # use std::cmp::Ordering;
1295+ use std::cmp::Ordering;
1296+
12851297fn cmp(a: int, b: int) -> Ordering {
12861298 if a < b { Ordering::Less }
12871299 else if a > b { Ordering::Greater }
@@ -1307,7 +1319,8 @@ fn main() {
13071319We can re-write this as a ` match ` :
13081320
13091321``` {rust}
1310- # use std::cmp::Ordering;
1322+ use std::cmp::Ordering;
1323+
13111324fn cmp(a: int, b: int) -> Ordering {
13121325 if a < b { Ordering::Less }
13131326 else if a > b { Ordering::Greater }
@@ -1368,7 +1381,8 @@ side of a `let` binding or directly where an expression is used. We could
13681381also implement the previous line like this:
13691382
13701383``` {rust}
1371- # use std::cmp::Ordering;
1384+ use std::cmp::Ordering;
1385+
13721386fn cmp(a: int, b: int) -> Ordering {
13731387 if a < b { Ordering::Less }
13741388 else if a > b { Ordering::Greater }
0 commit comments