Skip to content

Commit 38f5bee

Browse files
steveklabnikalexcrichton
authored andcommitted
Properly deal with Ordering in the guide
Now that it's been removed from the prelude, we need to treat things differently. Fixes #17967
1 parent c74be63 commit 38f5bee

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

src/doc/guide.md

+48-34
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,17 @@ enum Ordering {
11001100
```
11011101

11021102
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`:
11041110

11051111
```{rust}
1106-
# use std::cmp::Ordering;
1112+
use std::cmp::Ordering;
1113+
11071114
fn cmp(a: int, b: int) -> Ordering {
11081115
if a < b { Ordering::Less }
11091116
else if a > b { Ordering::Greater }
@@ -1126,18 +1133,25 @@ fn main() {
11261133
}
11271134
```
11281135

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.
11321141

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`.
11361148

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.
11411155

11421156
This enum has two variants, one of which has a value:
11431157

@@ -1170,18 +1184,19 @@ enum StringResult {
11701184
ErrorReason(String),
11711185
}
11721186
```
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.
11761191

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`:
11791193

11801194
```rust
1181-
# enum StringResult {
1182-
# StringOK(String),
1183-
# ErrorReason(String),
1184-
# }
1195+
enum StringResult {
1196+
StringOK(String),
1197+
ErrorReason(String),
1198+
}
1199+
11851200
fn respond(greeting: &str) -> StringResult {
11861201
if greeting == "Hello" {
11871202
StringResult::StringOK("Good morning!".to_string())
@@ -1191,10 +1206,7 @@ fn respond(greeting: &str) -> StringResult {
11911206
}
11921207
```
11931208

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:
11981210

11991211
```rust
12001212
use StringResult::StringOK;
@@ -1216,12 +1228,11 @@ fn respond(greeting: &str) -> StringResult {
12161228
}
12171229
```
12181230

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.
12251236

12261237
As you can see, `enum`s with values are quite a powerful tool for data representation,
12271238
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.
12751286
section on enums?
12761287

12771288
```{rust}
1278-
# use std::cmp::Ordering;
1289+
use std::cmp::Ordering;
1290+
12791291
fn cmp(a: int, b: int) -> Ordering {
12801292
if a < b { Ordering::Less }
12811293
else if a > b { Ordering::Greater }
@@ -1301,7 +1313,8 @@ fn main() {
13011313
We can re-write this as a `match`:
13021314

13031315
```{rust}
1304-
# use std::cmp::Ordering;
1316+
use std::cmp::Ordering;
1317+
13051318
fn cmp(a: int, b: int) -> Ordering {
13061319
if a < b { Ordering::Less }
13071320
else if a > b { Ordering::Greater }
@@ -1362,7 +1375,8 @@ side of a `let` binding or directly where an expression is used. We could
13621375
also implement the previous line like this:
13631376

13641377
```{rust}
1365-
# use std::cmp::Ordering;
1378+
use std::cmp::Ordering;
1379+
13661380
fn cmp(a: int, b: int) -> Ordering {
13671381
if a < b { Ordering::Less }
13681382
else if a > b { Ordering::Greater }

0 commit comments

Comments
 (0)