Rollup merge of #29613 - steveklabnik:29506, r=alexcrichton

in favor of just expect()

Fixes #29506
This commit is contained in:
Steve Klabnik 2015-11-05 22:28:49 +01:00
commit 15dd4170ff

View File

@ -83,7 +83,6 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line"); .expect("Failed to read line");
println!("You guessed: {}", guess); println!("You guessed: {}", guess);
@ -189,7 +188,6 @@ Lets move forward:
```rust,ignore ```rust,ignore
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line"); .expect("Failed to read line");
``` ```
@ -245,7 +243,6 @@ a single line of text, its only the first part of the single logical line of
code: code:
```rust,ignore ```rust,ignore
.ok()
.expect("Failed to read line"); .expect("Failed to read line");
``` ```
@ -254,33 +251,27 @@ and other whitespace. This helps you split up long lines. We _could_ have
done: done:
```rust,ignore ```rust,ignore
io::stdin().read_line(&mut guess).ok().expect("failed to read line"); io::stdin().read_line(&mut guess).expect("failed to read line");
``` ```
But that gets hard to read. So weve split it up, three lines for three But that gets hard to read. So weve split it up, three lines for three method
method calls. We already talked about `read_line()`, but what about `ok()` calls. We already talked about `read_line()`, but what about `expect()`? Well,
and `expect()`? Well, we already mentioned that `read_line()` puts what we already mentioned that `read_line()` puts what the user types into the `&mut
the user types into the `&mut String` we pass it. But it also returns String` we pass it. But it also returns a value: in this case, an
a value: in this case, an [`io::Result`][ioresult]. Rust has a number of [`io::Result`][ioresult]. Rust has a number of types named `Result` in its
types named `Result` in its standard library: a generic [`Result`][result], standard library: a generic [`Result`][result], and then specific versions for
and then specific versions for sub-libraries, like `io::Result`. sub-libraries, like `io::Result`.
[ioresult]: ../std/io/type.Result.html [ioresult]: ../std/io/type.Result.html
[result]: ../std/result/enum.Result.html [result]: ../std/result/enum.Result.html
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 `ok()` method, which says we want to assume this case, `io::Result` has an [`expect()` method][expect] that takes a value
this value is a successful one. If not, just throw away the error its called on, and if it isnt a successful one, [`panic!`][panic]s with a
information. Why throw it away? Well, for a basic program, we just want to message you passed it. A `panic!` like this will cause our program to crash,
print a generic error, as basically any issue means we cant continue. The displaying the message.
[`ok()` method][ok] returns a value which has another method defined on it:
`expect()`. The [`expect()` method][expect] takes a value its called on, and
if it isnt a successful one, [`panic!`][panic]s with a message you
passed it. A `panic!` like this will cause our program to crash, displaying
the message.
[ok]: ../std/result/enum.Result.html#method.ok
[expect]: ../std/option/enum.Option.html#method.expect [expect]: ../std/option/enum.Option.html#method.expect
[panic]: error-handling.html [panic]: error-handling.html
@ -468,7 +459,6 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
println!("You guessed: {}", guess); println!("You guessed: {}", guess);
@ -557,7 +547,6 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
println!("You guessed: {}", guess); println!("You guessed: {}", guess);
@ -668,11 +657,9 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
let guess: u32 = guess.trim().parse() let guess: u32 = guess.trim().parse()
.ok()
.expect("Please type a number!"); .expect("Please type a number!");
println!("You guessed: {}", guess); println!("You guessed: {}", guess);
@ -689,7 +676,6 @@ The new three lines:
```rust,ignore ```rust,ignore
let guess: u32 = guess.trim().parse() let guess: u32 = guess.trim().parse()
.ok()
.expect("Please type a number!"); .expect("Please type a number!");
``` ```
@ -706,27 +692,26 @@ We bind `guess` to an expression that looks like something we wrote earlier:
guess.trim().parse() guess.trim().parse()
``` ```
Followed by an `ok().expect()` invocation. Here, `guess` refers to the old Here, `guess` refers to the old `guess`, the one that was a `String` with our
`guess`, the one that was a `String` with our input in it. The `trim()` input in it. The `trim()` method on `String`s will eliminate any white space at
method on `String`s will eliminate any white space at the beginning and end of the beginning and end of our string. This is important, as we had to press the
our string. This is important, as we had to press the return key to satisfy return key to satisfy `read_line()`. This means that if we type `5` and hit
`read_line()`. This means that if we type `5` and hit return, `guess` looks return, `guess` looks like this: `5\n`. The `\n` represents newline, the
like this: `5\n`. The `\n` represents newline, the enter key. `trim()` gets enter key. `trim()` gets rid of this, leaving our string with just the `5`. The
rid of this, leaving our string with just the `5`. The [`parse()` method on [`parse()` method on strings][parse] parses a string into some kind of number.
strings][parse] parses a string into some kind of number. Since it can parse a Since it can parse a variety of numbers, we need to give Rust a hint as to the
variety of numbers, we need to give Rust a hint as to the exact type of number exact type of number we want. Hence, `let guess: u32`. The colon (`:`) after
we want. Hence, `let guess: u32`. The colon (`:`) after `guess` tells Rust `guess` tells Rust were going to annotate its type. `u32` is an unsigned,
were going to annotate its type. `u32` is an unsigned, thirty-two bit thirty-two bit integer. Rust has [a number of built-in number types][number],
integer. Rust has [a number of built-in number types][number], but weve but weve chosen `u32`. Its a good default choice for a small positive number.
chosen `u32`. Its a good default choice for a small positive number.
[parse]: ../std/primitive.str.html#method.parse [parse]: ../std/primitive.str.html#method.parse
[number]: primitive-types.html#numeric-types [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
such, well do the same thing we did with `read_line()`: use the `ok()` and such, well do the same thing we did with `read_line()`: use the `expect()`
`expect()` methods to crash if theres an error. method to crash if theres an error.
Lets try our program out! Lets try our program out!
@ -773,11 +758,9 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
let guess: u32 = guess.trim().parse() let guess: u32 = guess.trim().parse()
.ok()
.expect("Please type a number!"); .expect("Please type a number!");
println!("You guessed: {}", guess); println!("You guessed: {}", guess);
@ -841,11 +824,9 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
let guess: u32 = guess.trim().parse() let guess: u32 = guess.trim().parse()
.ok()
.expect("Please type a number!"); .expect("Please type a number!");
println!("You guessed: {}", guess); println!("You guessed: {}", guess);
@ -888,7 +869,6 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
let guess: u32 = match guess.trim().parse() { let guess: u32 = match guess.trim().parse() {
@ -920,7 +900,6 @@ let guess: u32 = match guess.trim().parse() {
``` ```
This is how you generally move from crash on error to actually handle the This is how you generally move from crash on error to actually handle the
error, by switching from `ok().expect()` to a `match` statement. The `Result`
returned by `parse()` is an `enum` just like `Ordering`, but in this case, each returned by `parse()` is an `enum` just like `Ordering`, but in this case, each
variant has some data associated with it: `Ok` is a success, and `Err` is a variant has some data associated with it: `Ok` is a success, and `Err` is a
failure. Each contains more information: the successfully parsed integer, or an failure. Each contains more information: the successfully parsed integer, or an
@ -977,7 +956,6 @@ fn main() {
let mut guess = String::new(); let mut guess = String::new();
io::stdin().read_line(&mut guess) io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line"); .expect("failed to read line");
let guess: u32 = match guess.trim().parse() { let guess: u32 = match guess.trim().parse() {