Remove or change all forward refs to "Chapter XX"

This commit is contained in:
Carol (Nichols || Goulding) 2016-07-12 16:04:18 -04:00
parent 60c9abaf39
commit da2dd5b09f

View File

@ -116,9 +116,7 @@ As youve seen in Chapter 1, the `main()` function is the entry point into the
program. The `fn` syntax declares a new function, the `()`s indicate that program. The `fn` syntax declares a new function, the `()`s indicate that
there are no arguments, and `{` starts the body of the function. Because there are no arguments, and `{` starts the body of the function. Because
we didnt include a return type, its assumed to be `()`, an empty we didnt include a return type, its assumed to be `()`, an empty
[tuple][tuples]. tuple. We will go over tuples in Chapter XX.
[tuples]: primitive-types.html#tuples
```rust,ignore ```rust,ignore
println!("Guess the number!"); println!("Guess the number!");
@ -126,48 +124,36 @@ println!("Guess the number!");
println!("Please input your guess."); println!("Please input your guess.");
``` ```
We previously learned in Chapter 1 that `println!()` is a [macro][macros] that We previously learned in Chapter 1 that `println!()` is a macro that
prints a [string][strings] to the screen. prints a string to the screen.
[macros]: macros.html
[strings]: strings.html
```rust,ignore ```rust,ignore
let mut guess = String::new(); let mut guess = String::new();
``` ```
Now were getting interesting! Theres a lot going on in this little line. Now were getting interesting! Theres a lot going on in this little line.
The first thing to notice is that this is a [let statement][let], which is The first thing to notice is that this is a let statement, which is
used to create variable bindings. They take this form: used to create variable bindings. Variable bindings will be covered in more
detail in Chapter XX. Here's an example:
```rust,ignore ```rust,ignore
let foo = bar; let foo = bar;
``` ```
[let]: variable-bindings.html
This will create a new binding named `foo`, and bind it to the value `bar`. In This will create a new binding named `foo`, and bind it to the value `bar`. In
many languages, this is called a variable, but Rusts variable bindings have many languages, this is called a variable, but Rusts variable bindings have
a few tricks up their sleeves. a few tricks up their sleeves.
For example, theyre [immutable][immutable] by default. Thats why our example For example, theyre immutable by default. Thats why our example
uses `mut`: it makes a binding mutable, rather than immutable. Also, `let` uses `mut`: it makes a binding mutable, rather than immutable.
doesnt actually take a name on the left hand side of the assignment, it really
accepts a [pattern][patterns]. Well use more complicated patterns later; for
now, let's use only a name:
```rust ```rust
let foo = 5; // immutable. let foo = 5; // immutable.
let mut bar = 5; // mutable let mut bar = 5; // mutable
``` ```
[immutable]: mutability.html
[patterns]: patterns.html
Oh, and `//` will start a comment, until the end of the line. Rust ignores Oh, and `//` will start a comment, until the end of the line. Rust ignores
everything in [comments][comments]. everything in comments.
[comments]: comments.html
So now we know that `let mut guess` will introduce a mutable binding named So now we know that `let mut guess` will introduce a mutable binding named
`guess`, but we have to look at the other side of the `=` for what its `guess`, but we have to look at the other side of the `=` for what its
@ -216,30 +202,28 @@ The next part will use this handle to get input from the user:
.read_line(&mut guess) .read_line(&mut guess)
``` ```
Here, we call the [`read_line()`][read_line] method on our handle. Here, we call the [`read_line()`][read_line] method on our handle. Methods are
[Methods][method] are like associated functions but are only available on a like associated functions but are only available on a particular instance of a
particular instance of a type, rather than the type itself. Were also passing type, rather than the type itself. We'll talk more about methods in Chapter XX.
one argument to `read_line()`: `&mut guess`. Were also passing one argument to `read_line()`: `&mut guess`.
[read_line]: ../std/io/struct.Stdin.html#method.read_line [read_line]: ../std/io/struct.Stdin.html#method.read_line
[method]: method-syntax.html
Remember how we bound `guess` above? We said it was mutable. However, Remember how we bound `guess` above? We said it was mutable. However,
`read_line` doesnt take a `String` as an argument: it takes a `&mut String`. `read_line` doesnt take a `String` as an argument: it takes a `&mut String`.
Rust has a feature called [references][references], which allows you to have The `&` is the feature of Rust called a reference, which allows you to have
multiple references to one piece of data, which can reduce copying. References multiple ways to access one piece of data in order to reduce copying.
are a complex feature, as one of Rusts major selling points is how safe and References are a complex feature, as one of Rusts major selling points is how
easy it is to use references. We dont need to know a lot of those details to safe and easy it is to use references. We dont need to know a lot of those
finish our program right now, though. For now, all we need to know is that details to finish our program right now, though; Chapter XX will cover them in
like `let` bindings, references are immutable by default. Hence, we need to more detail. For now, all we need to know is that like `let` bindings,
write `&mut guess`, rather than `&guess`. references are immutable by default. Hence, we need to write `&mut guess`,
rather than `&guess`.
Why does `read_line()` take a mutable reference to a string? Its job is Why does `read_line()` take a mutable reference to a string? Its job is
to take what the user types into standard input, and place that into a to take what the user types into standard input and place that into a
string. So it takes that string as an argument, and in order to add string. So it takes that string as an argument, and in order to add
the input, it needs to be mutable. the input, that string needs to be mutable.
[references]: references-and-borrowing.html
But were not quite done with this line of code, though. While its But were not quite done with this line of code, though. While its
a single line of text, its only the first part of the single logical line of a single line of text, its only the first part of the single logical line of
@ -271,12 +255,11 @@ sub-libraries, like `io::Result`.
The purpose of these `Result` types is to encode error handling information. The purpose of these `Result` types is to encode error handling information.
Values of the `Result` type, like any type, have methods defined on them. In Values of the `Result` type, like any type, have methods defined on them. In
this case, `io::Result` has an [`expect()` method][expect] that takes a value this case, `io::Result` has an [`expect()` method][expect] that takes a value
its called on, and if it isnt a successful one, [`panic!`][panic]s with a its called on, and if it isnt a successful one, `panic!`s with a
message you passed it. A `panic!` like this will cause our program to crash, message you passed it. A `panic!` like this will cause our program to crash,
displaying the message. displaying the message.
[expect]: ../std/result/enum.Result.html#method.expect [expect]: ../std/result/enum.Result.html#method.expect
[panic]: error-handling.html
If we don't call this method, our program will compile, but well get a warning: If we don't call this method, our program will compile, but well get a warning:
@ -369,7 +352,7 @@ documentation][cargodoc] contains more details and other ways to specify
dependencies. dependencies.
[semver]: http://semver.org [semver]: http://semver.org
[cargodoc]: http://doc.crates.io/crates-io.html [cargodoc]: http://doc.crates.io/specifying-dependencies.html
Now, without changing any of our code, lets build our project: Now, without changing any of our code, lets build our project:
@ -383,7 +366,8 @@ $ cargo build
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
``` ```
(You may see different versions and the lines may be in a different order.) You may see different versions (but they will be compatible, thanks to semver!)
and the lines may be in a different order.
Lots of new output! Now that we have an external dependency, Cargo fetches the Lots of new output! Now that we have an external dependency, Cargo fetches the
latest versions of everything from the *registry*, which is a copy of data from latest versions of everything from the *registry*, which is a copy of data from
@ -439,9 +423,10 @@ do, the next time we `cargo build`, Cargo will update the index and re-evaluate
our `rand` requirements. our `rand` requirements.
Theres a lot more to say about [Cargo][doccargo] and [its Theres a lot more to say about [Cargo][doccargo] and [its
ecosystem][doccratesio], but for now, thats all we need to know. Cargo makes ecosystem][doccratesio] that we will get into in Chapter XX, but for now,
it really easy to re-use libraries, so Rustaceans are able to write smaller thats all we need to know. Cargo makes it really easy to re-use libraries, so
projects which are assembled out of a number of sub-packages. Rustaceans are able to write smaller projects which are assembled out of a
number of sub-packages.
[doccargo]: http://doc.crates.io [doccargo]: http://doc.crates.io
[doccratesio]: http://doc.crates.io/crates-io.html [doccratesio]: http://doc.crates.io/crates-io.html
@ -482,9 +467,7 @@ Next, we added another `use` line: `use rand::Rng`. Were going to use a
method in a moment, and it requires that `Rng` be in scope to work. The basic method in a moment, and it requires that `Rng` be in scope to work. The basic
idea is this: methods are defined on something called traits, and for the idea is this: methods are defined on something called traits, and for the
method to work, it needs the trait to be in scope. For more about the method to work, it needs the trait to be in scope. For more about the
details, read the [traits][traits] section. details, read the traits section in Chapter XX.
[traits]: traits.html
There are two other lines we added, in the middle: There are two other lines we added, in the middle:
@ -495,15 +478,13 @@ println!("The secret number is: {}", secret_number);
``` ```
We use the `rand::thread_rng()` function to get a copy of the random number We use the `rand::thread_rng()` function to get a copy of the random number
generator, which is local to the particular [thread][concurrency] of execution generator, which is local to the particular thread of execution
were in. Because we put `use rand::Rng` above, the random number generator has were in. Because we put `use rand::Rng` above, the random number generator has
a `gen_range()` method available. This method takes two numbers as arguments a `gen_range()` method available. This method takes two numbers as arguments
and generates a random number between them. Its inclusive on the lower bound and generates a random number between them. Its inclusive on the lower bound
but exclusive on the upper bound, so we need `1` and `101` to ask for a number but exclusive on the upper bound, so we need `1` and `101` to ask for a number
ranging from one to a hundred. ranging from one to a hundred.
[concurrency]: concurrency.html
The second line prints out the secret number. This is useful while The second line prints out the secret number. This is useful while
were developing our program to let us easily test it out, but well be were developing our program to let us easily test it out, but well be
deleting it for the final version. Its not much of a game if it prints out deleting it for the final version. Its not much of a game if it prints out
@ -714,12 +695,11 @@ The [`parse()` method on strings][parse] parses a string into some kind of
number. Since it can parse a variety of numbers, we need to give Rust a hint as number. Since it can parse a variety of numbers, we need to give Rust a hint as
to the exact type of number we want. Hence, `let guess: u32`. The colon (`:`) to the exact type of number we want. Hence, `let guess: u32`. The colon (`:`)
after `guess` tells Rust were going to annotate its type. `u32` is an after `guess` tells Rust were going to annotate its type. `u32` is an
unsigned, thirty-two bit integer. Rust has [a number of built-in number unsigned, thirty-two bit integer. Rust has a number of built-in number
types][number], but weve chosen `u32`. Its a good default choice for a small types, but weve chosen `u32`. Its a good default choice for a small
positive number. positive number. You'll see the other number types in Chapter XX.
[parse]: ../std/primitive.str.html#method.parse [parse]: ../std/primitive.str.html#method.parse
[number]: primitive-types.html#numeric-types
Just like `read_line()`, our call to `parse()` could cause an error. What if Just like `read_line()`, our call to `parse()` could cause an error. What if
our string contained `A👍%`? Thered be no way to convert that to a number. As our string contained `A👍%`? Thered be no way to convert that to a number. As