Resolve guessing game tutorial comments

Fixes #154.
This commit is contained in:
Carol (Nichols || Goulding) 2016-08-01 22:34:10 -04:00
parent da3283539c
commit 82e8759bac

View File

@ -1,18 +1,14 @@
# Guessing Game # Guessing Game
Lets learn some Rust! For our first project, well implement a classic Let's jump into Rust with a hands-on project! Well implement a classic
beginner programming problem: the guessing game. Heres how it works: Our beginner programming problem: the guessing game. Heres how it works: Our
program will generate a random integer between one and a hundred. It will then program will generate a random integer between one and a hundred. It will then
prompt us to enter a guess. Upon entering our guess, it will tell us if were prompt us to enter a guess. Upon entering our guess, it will tell us if were
too low or too high. Once we guess correctly, it will congratulate us. Sound too low or too high. Once we guess correctly, it will congratulate us.
good?
## Set up ## Set up
Lets set up a new project. Go to your projects directory. Remember the end of Lets set up a new project. Go to your projects directory, and create a new project using Cargo.
the `hello world` example that mentioned `cargo new` to create new cargo
projects? Lets give it a shot:
```bash ```bash
$ cd ~/projects $ cd ~/projects
@ -21,7 +17,7 @@ $ cd guessing_game
``` ```
We pass the name of our project to `cargo new`, then the `--bin` flag, since We pass the name of our project to `cargo new`, then the `--bin` flag, since
were making a binary, rather than a library. were going to be making another binary like in Chapter 1.
Take a look at the generated `Cargo.toml`: Take a look at the generated `Cargo.toml`:
@ -34,10 +30,11 @@ authors = ["Your Name <you@example.com>"]
[dependencies] [dependencies]
``` ```
Cargo gets this information from your environment. If its not correct, go ahead If the authors information that Cargo got from your environment is not correct,
and fix that. go ahead and fix that.
Finally, Cargo generated a Hello, world! for us. Check out `src/main.rs`: And as we saw in the last chapter, `cargo new` generates a Hello, world! for
us. Check out `src/main.rs`:
```rust ```rust
fn main() { fn main() {
@ -45,16 +42,7 @@ fn main() {
} }
``` ```
Lets try compiling what Cargo gave us: Lets try compiling what Cargo gave us and running it in the same step, using the `cargo run` command:
```bash
$ cargo build
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
```
Before we move on, let me show you one more Cargo command: `run`. `cargo run`
is kind of like `cargo build`, but it also then runs the produced executable.
Try it out:
```bash ```bash
$ cargo run $ cargo run
@ -101,12 +89,10 @@ use std::io;
Well need to take user input and then print the result as output. As such, we Well need to take user input and then print the result as output. As such, we
need the `io` library from the standard library. Rust only imports a few things need the `io` library from the standard library. Rust only imports a few things
by default into every program, [the prelude][prelude]. If its not in the by default into every program, [the prelude][prelude]. If its not in the
prelude, youll have to `use` it directly. There is also a second prelude, the prelude, youll have to `use` it directly. Using the `std::io` library gets
[`io` prelude][ioprelude], which serves a similar function: when you import it, you a number of useful `io`-related things, so that's what we've done here.
you get a number of useful, `io`-related things, so that's what we've done here.
[prelude]: ../std/prelude/index.html [prelude]: ../std/prelude/index.html
[ioprelude]: ../std/io/prelude/index.html
```rust,ignore ```rust,ignore
fn main() { fn main() {
@ -114,9 +100,7 @@ fn main() {
As youve seen in Chapter 1, the `main()` function is the entry point into the 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.
we didnt include a return type, its assumed to be `()`, an empty
tuple. We will go over tuples in Chapter XX.
```rust,ignore ```rust,ignore
println!("Guess the number!"); println!("Guess the number!");
@ -179,7 +163,7 @@ io::stdin().read_line(&mut guess)
.expect("Failed to read line"); .expect("Failed to read line");
``` ```
Thats a lot more! Lets go bit-by-bit. The first line has two parts. Heres Lets go through this together bit-by-bit. The first line has two parts. Heres
the first: the first:
```rust,ignore ```rust,ignore
@ -201,10 +185,8 @@ 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. Methods are Here, we call the [`read_line()`][read_line] method on our handle. Were also
like associated functions but are only available on a particular instance of a passing one argument to `read_line()`: `&mut guess`.
type, rather than the type itself. We'll talk more about methods in Chapter XX.
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
@ -254,9 +236,8 @@ 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!`s with a its called on, and if it isnt a successful result, will cause our program to
message you passed it. A `panic!` like this will cause our program to crash, crash and display the message that we passed as an argument to `expect()`.
displaying the message.
[expect]: ../std/result/enum.Result.html#method.expect [expect]: ../std/result/enum.Result.html#method.expect
@ -342,19 +323,12 @@ everything that follows the section heading is part of that section, until
another section starts. Cargo uses the dependencies section to know what another section starts. Cargo uses the dependencies section to know what
dependencies on external crates you have and what versions of those crates you dependencies on external crates you have and what versions of those crates you
require. In this case, weve specified the `rand` crate with the semantic require. In this case, weve specified the `rand` crate with the semantic
version specifier `0.3.14`. version specifier `0.3.14`. Cargo understands [Semantic Versioning][semver], a
standard for writing version numbers. A bare number like above is actually
Cargo understands [Semantic Versioning][semver], which is a standard for shorthand for `^0.3.14`, which means "any version that has a public API
writing version numbers. A bare number like above is actually shorthand for compatible with version 0.3.14".
`^0.3.14`, which means "any version that has a public API compatible with
version 0.3.14". If we wanted to use only `0.3.14` exactly, we could say `rand
= "=0.3.14"` (note the equal sign within the version string). And if we wanted
to use whatever the latest version currently is, we could use `*`. [Cargos
documentation][cargodoc] contains more details and other ways to specify
dependencies.
[semver]: http://semver.org [semver]: http://semver.org
[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:
@ -770,8 +744,8 @@ fn main() {
``` ```
And try it out. But wait, didnt we just add an infinite loop? Yup. Remember And try it out. But wait, didnt we just add an infinite loop? Yup. Remember
our discussion about `parse()`? If we give a non-number answer, well `panic!` our discussion about `parse()`? If we give a non-number answer, the program
and quit. Observe: will crash and, therefore, quit. Observe:
```bash ```bash
$ cargo run $ cargo run