Skip to content

Commit 50cbf0a

Browse files
steveklabnikalexcrichton
authored andcommitted
---
yaml --- r: 167531 b: refs/heads/snap-stage3 c: 76e3bc2 h: refs/heads/master i: 167529: 0410921 167527: 014b2eb v: v3
1 parent 243911c commit 50cbf0a

File tree

5 files changed

+57
-41
lines changed

5 files changed

+57
-41
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 023dfb0c898d851dee6ace2f8339b73b5287136b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 56290a004493a5d2e211f056601533253497df60
4+
refs/heads/snap-stage3: 76e3bc23388e268438e4318b0580149619a9d1ac
55
refs/heads/try: 5204084bd2e46af7cc6e0147430e44dd0d657bbb
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d

branches/snap-stage3/src/doc/guide.md

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,17 @@ enum Ordering {
11061106
```
11071107

11081108
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`:
11101116

11111117
```{rust}
1112-
# use std::cmp::Ordering;
1118+
use std::cmp::Ordering;
1119+
11131120
fn 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

11481162
This 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+
11911206
fn 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
12061218
use 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

12321243
As you can see, `enum`s with values are quite a powerful tool for data representation,
12331244
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.
12811292
section on enums?
12821293

12831294
```{rust}
1284-
# use std::cmp::Ordering;
1295+
use std::cmp::Ordering;
1296+
12851297
fn 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() {
13071319
We can re-write this as a `match`:
13081320

13091321
```{rust}
1310-
# use std::cmp::Ordering;
1322+
use std::cmp::Ordering;
1323+
13111324
fn 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
13681381
also implement the previous line like this:
13691382

13701383
```{rust}
1371-
# use std::cmp::Ordering;
1384+
use std::cmp::Ordering;
1385+
13721386
fn cmp(a: int, b: int) -> Ordering {
13731387
if a < b { Ordering::Less }
13741388
else if a > b { Ordering::Greater }

branches/snap-stage3/src/libstd/sys/windows/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ fn free_handle(handle: *mut ()) {
467467

468468
#[cfg(test)]
469469
mod tests {
470+
use c_str::ToCStr;
470471

471472
#[test]
472473
fn test_make_command_line() {

branches/snap-stage3/src/test/run-pass/bool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// Basic boolean tests
1212

13-
use std::cmp::{Equal, Greater, Less};
13+
use std::cmp::Ordering::{Equal, Greater, Less};
1414
use std::ops::{BitAnd, BitOr, BitXor};
1515

1616
fn main() {

branches/snap-stage3/src/test/run-pass/tcp-stress.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717
extern crate log;
1818
extern crate libc;
1919

20+
use std::comm::channel;
2021
use std::io::net::tcp::{TcpListener, TcpStream};
2122
use std::io::{Acceptor, Listener};
22-
use std::thread::Builder;
23+
use std::thread::{Builder, Thread};
2324
use std::time::Duration;
2425

2526
fn main() {
2627
// This test has a chance to time out, try to not let it time out
27-
spawn(move|| {
28+
Thread::spawn(move|| -> () {
2829
use std::io::timer;
2930
timer::sleep(Duration::milliseconds(30 * 1000));
3031
println!("timed out!");
3132
unsafe { libc::exit(1) }
32-
});
33+
}).detach();
3334

3435
let (tx, rx) = channel();
35-
spawn(move|| {
36+
Thread::spawn(move || -> () {
3637
let mut listener = TcpListener::bind("127.0.0.1:0").unwrap();
3738
tx.send(listener.socket_name().unwrap());
3839
let mut acceptor = listener.listen();
@@ -47,7 +48,7 @@ fn main() {
4748
stream.read_byte();
4849
stream.write(&[2]);
4950
}
50-
});
51+
}).detach();
5152
let addr = rx.recv();
5253

5354
let (tx, rx) = channel();

0 commit comments

Comments
 (0)