mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-25 00:18:46 +08:00
Small tweaks to things I wanted to change while reading through again
This commit is contained in:
parent
ed94e466db
commit
b0126e2c97
@ -8,7 +8,7 @@ tradition.
|
|||||||
> Note: This book assumes basic familiarity with the command line. Rust itself
|
> Note: This book assumes basic familiarity with the command line. Rust itself
|
||||||
> makes no specific demands about your editing, tooling, or where your code
|
> makes no specific demands about your editing, tooling, or where your code
|
||||||
> lives, so if you prefer an IDE to the command line, feel free to use your
|
> lives, so if you prefer an IDE to the command line, feel free to use your
|
||||||
> favored IDE.
|
> favorite IDE.
|
||||||
|
|
||||||
### Creating a Project File
|
### Creating a Project File
|
||||||
|
|
||||||
|
@ -247,8 +247,8 @@ calls. Now let's see what this line does.
|
|||||||
|
|
||||||
### Handling Potential Failure with the `Result` Type
|
### Handling Potential Failure with the `Result` Type
|
||||||
|
|
||||||
We mentioned that `read_line()` puts what the user types into the `&mut
|
We mentioned that `read_line()` puts what the user types into the string we
|
||||||
String` we pass it, but it also returns a value: in this case, an
|
pass it, but it also returns a value: in this case, an
|
||||||
[`io::Result`][ioresult]. Rust has a number of types named `Result` in its
|
[`io::Result`][ioresult]. Rust has a number of types named `Result` in its
|
||||||
standard library: a generic [`Result`][result], and then specific versions for
|
standard library: a generic [`Result`][result], and then specific versions for
|
||||||
sub-libraries, like `io::Result`.
|
sub-libraries, like `io::Result`.
|
||||||
@ -305,7 +305,6 @@ brace:
|
|||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
println!("You guessed: {}", guess);
|
println!("You guessed: {}", guess);
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This prints out the string we saved our input in. The `{}`s are a placeholder:
|
This prints out the string we saved our input in. The `{}`s are a placeholder:
|
||||||
@ -744,9 +743,9 @@ let guess: u32 = guess.trim().parse()
|
|||||||
We create a variable binding `guess`. But wait a minute, don't we already have
|
We create a variable binding `guess`. But wait a minute, don't we already have
|
||||||
a variable binding named `guess`? We do, but Rust allows us to *shadow* the
|
a variable binding named `guess`? We do, but Rust allows us to *shadow* the
|
||||||
previous value of `guess` with a new one. This is often used in this exact
|
previous value of `guess` with a new one. This is often used in this exact
|
||||||
situation, where we want to convert a string to a numerical type. Shadowing
|
situation, where we want to convert a value from one type into another type.
|
||||||
lets us re-use the `guess` variable name rather than forcing us to come up with
|
Shadowing lets us re-use the `guess` variable name rather than forcing us to
|
||||||
two unique bindings, like `guess_str` and `guess` or something.
|
come up with two unique bindings, like `guess_str` and `guess` or something.
|
||||||
|
|
||||||
We bind `guess` to the expression `guess.trim().parse()`.
|
We bind `guess` to the expression `guess.trim().parse()`.
|
||||||
|
|
||||||
@ -760,9 +759,9 @@ represents "newline", the return key. The `trim()` method gets rid of this,
|
|||||||
leaving our string with only the `5`.
|
leaving our string with only the `5`.
|
||||||
|
|
||||||
The [`parse()` method on strings][parse] parses a string into some kind of
|
The [`parse()` method on strings][parse] parses a string into some kind of
|
||||||
number. Since it can parse a variety of number types, we need to tell Rust the
|
number. Since this method can parse a variety of number types, we need to tell
|
||||||
exact type of number we want with `let guess: u32`. The colon (`:`) after
|
Rust the exact type of number we want with `let guess: u32`. The colon (`:`)
|
||||||
`guess` tells Rust we’re going to annotate its type. Rust has a few
|
after `guess` tells Rust we’re going to annotate its type. Rust has a few
|
||||||
built-in number types, but we’ve chosen `u32`, an unsigned, thirty-two bit
|
built-in number types, but we’ve chosen `u32`, an unsigned, thirty-two bit
|
||||||
integer. It’s a good default choice for a small positive number. You'll see the
|
integer. It’s a good default choice for a small positive number. You'll see the
|
||||||
other number types in Chapter XX.
|
other number types in Chapter XX.
|
||||||
@ -800,7 +799,7 @@ behavior with different kinds of input: guess the number correctly, guess a
|
|||||||
number that is too high, and guess a number that is too low.
|
number that is too high, and guess a number that is too low.
|
||||||
|
|
||||||
Now we’ve got most of the game working, but we can only make one guess. Let’s
|
Now we’ve got most of the game working, but we can only make one guess. Let’s
|
||||||
change that by adding loops!
|
change that by adding a loop!
|
||||||
|
|
||||||
## Allowing Multiple Guesses with Looping
|
## Allowing Multiple Guesses with Looping
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user