Skip to content

Commit 6d9502d

Browse files
committed
Remove .ok().expect()
in favor of just expect() Fixes #29506
1 parent a216e84 commit 6d9502d

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

src/doc/trpl/guessing-game.md

+27-49
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ fn main() {
8383
let mut guess = String::new();
8484
8585
io::stdin().read_line(&mut guess)
86-
.ok()
8786
.expect("Failed to read line");
8887
8988
println!("You guessed: {}", guess);
@@ -189,7 +188,6 @@ Let’s move forward:
189188

190189
```rust,ignore
191190
io::stdin().read_line(&mut guess)
192-
.ok()
193191
.expect("Failed to read line");
194192
```
195193

@@ -245,7 +243,6 @@ a single line of text, it’s only the first part of the single logical line of
245243
code:
246244

247245
```rust,ignore
248-
.ok()
249246
.expect("Failed to read line");
250247
```
251248

@@ -254,33 +251,27 @@ and other whitespace. This helps you split up long lines. We _could_ have
254251
done:
255252

256253
```rust,ignore
257-
io::stdin().read_line(&mut guess).ok().expect("failed to read line");
254+
io::stdin().read_line(&mut guess).expect("failed to read line");
258255
```
259256

260-
But that gets hard to read. So we’ve split it up, three lines for three
261-
method calls. We already talked about `read_line()`, but what about `ok()`
262-
and `expect()`? Well, we already mentioned that `read_line()` puts what
263-
the user types into the `&mut String` we pass it. But it also returns
264-
a value: in this case, an [`io::Result`][ioresult]. Rust has a number of
265-
types named `Result` in its standard library: a generic [`Result`][result],
266-
and then specific versions for sub-libraries, like `io::Result`.
257+
But that gets hard to read. So we’ve split it up, three lines for three method
258+
calls. We already talked about `read_line()`, but what about `expect()`? Well,
259+
we already mentioned that `read_line()` puts what the user types into the `&mut
260+
String` we pass it. But it also returns a value: in this case, an
261+
[`io::Result`][ioresult]. Rust has a number of types named `Result` in its
262+
standard library: a generic [`Result`][result], and then specific versions for
263+
sub-libraries, like `io::Result`.
267264

268265
[ioresult]: ../std/io/type.Result.html
269266
[result]: ../std/result/enum.Result.html
270267

271268
The purpose of these `Result` types is to encode error handling information.
272269
Values of the `Result` type, like any type, have methods defined on them. In
273-
this case, `io::Result` has an `ok()` method, which says ‘we want to assume
274-
this value is a successful one. If not, just throw away the error
275-
information’. Why throw it away? Well, for a basic program, we just want to
276-
print a generic error, as basically any issue means we can’t continue. The
277-
[`ok()` method][ok] returns a value which has another method defined on it:
278-
`expect()`. The [`expect()` method][expect] takes a value it’s called on, and
279-
if it isn’t a successful one, [`panic!`][panic]s with a message you
280-
passed it. A `panic!` like this will cause our program to crash, displaying
281-
the message.
282-
283-
[ok]: ../std/result/enum.Result.html#method.ok
270+
this case, `io::Result` has an [`expect()` method][expect] that takes a value
271+
it’s called on, and if it isn’t a successful one, [`panic!`][panic]s with a
272+
message you passed it. A `panic!` like this will cause our program to crash,
273+
displaying the message.
274+
284275
[expect]: ../std/option/enum.Option.html#method.expect
285276
[panic]: error-handling.html
286277

@@ -468,7 +459,6 @@ fn main() {
468459
let mut guess = String::new();
469460
470461
io::stdin().read_line(&mut guess)
471-
.ok()
472462
.expect("failed to read line");
473463
474464
println!("You guessed: {}", guess);
@@ -557,7 +547,6 @@ fn main() {
557547
let mut guess = String::new();
558548
559549
io::stdin().read_line(&mut guess)
560-
.ok()
561550
.expect("failed to read line");
562551
563552
println!("You guessed: {}", guess);
@@ -668,11 +657,9 @@ fn main() {
668657
let mut guess = String::new();
669658

670659
io::stdin().read_line(&mut guess)
671-
.ok()
672660
.expect("failed to read line");
673661

674662
let guess: u32 = guess.trim().parse()
675-
.ok()
676663
.expect("Please type a number!");
677664

678665
println!("You guessed: {}", guess);
@@ -689,7 +676,6 @@ The new three lines:
689676
690677
```rust,ignore
691678
let guess: u32 = guess.trim().parse()
692-
.ok()
693679
.expect("Please type a number!");
694680
```
695681
@@ -706,27 +692,26 @@ We bind `guess` to an expression that looks like something we wrote earlier:
706692
guess.trim().parse()
707693
```
708694
709-
Followed by an `ok().expect()` invocation. Here, `guess` refers to the old
710-
`guess`, the one that was a `String` with our input in it. The `trim()`
711-
method on `String`s will eliminate any white space at the beginning and end of
712-
our string. This is important, as we had to press the ‘return’ key to satisfy
713-
`read_line()`. This means that if we type `5` and hit return, `guess` looks
714-
like this: `5\n`. The `\n` represents ‘newline’, the enter key. `trim()` gets
715-
rid of this, leaving our string with just the `5`. The [`parse()` method on
716-
strings][parse] parses a string into some kind of number. Since it can parse a
717-
variety of numbers, we need to give Rust a hint as to the exact type of number
718-
we want. Hence, `let guess: u32`. The colon (`:`) after `guess` tells Rust
719-
we’re going to annotate its type. `u32` is an unsigned, thirty-two bit
720-
integer. Rust has [a number of built-in number types][number], but we’ve
721-
chosen `u32`. It’s a good default choice for a small positive number.
695+
Here, `guess` refers to the old `guess`, the one that was a `String` with our
696+
input in it. The `trim()` method on `String`s will eliminate any white space at
697+
the beginning and end of our string. This is important, as we had to press the
698+
‘return’ key to satisfy `read_line()`. This means that if we type `5` and hit
699+
return, `guess` looks like this: `5\n`. The `\n` represents ‘newline’, the
700+
enter key. `trim()` gets rid of this, leaving our string with just the `5`. The
701+
[`parse()` method on strings][parse] parses a string into some kind of number.
702+
Since it can parse a variety of numbers, we need to give Rust a hint as to the
703+
exact type of number we want. Hence, `let guess: u32`. The colon (`:`) after
704+
`guess` tells Rust we’re going to annotate its type. `u32` is an unsigned,
705+
thirty-two bit integer. Rust has [a number of built-in number types][number],
706+
but we’ve chosen `u32`. It’s a good default choice for a small positive number.
722707
723708
[parse]: ../std/primitive.str.html#method.parse
724709
[number]: primitive-types.html#numeric-types
725710
726711
Just like `read_line()`, our call to `parse()` could cause an error. What if
727712
our string contained `A👍%`? There’d be no way to convert that to a number. As
728-
such, we’ll do the same thing we did with `read_line()`: use the `ok()` and
729-
`expect()` methods to crash if there’s an error.
713+
such, we’ll do the same thing we did with `read_line()`: use the `expect()`
714+
method to crash if there’s an error.
730715
731716
Let’s try our program out!
732717
@@ -773,11 +758,9 @@ fn main() {
773758
let mut guess = String::new();
774759
775760
io::stdin().read_line(&mut guess)
776-
.ok()
777761
.expect("failed to read line");
778762
779763
let guess: u32 = guess.trim().parse()
780-
.ok()
781764
.expect("Please type a number!");
782765
783766
println!("You guessed: {}", guess);
@@ -841,11 +824,9 @@ fn main() {
841824
let mut guess = String::new();
842825
843826
io::stdin().read_line(&mut guess)
844-
.ok()
845827
.expect("failed to read line");
846828
847829
let guess: u32 = guess.trim().parse()
848-
.ok()
849830
.expect("Please type a number!");
850831
851832
println!("You guessed: {}", guess);
@@ -888,7 +869,6 @@ fn main() {
888869
let mut guess = String::new();
889870
890871
io::stdin().read_line(&mut guess)
891-
.ok()
892872
.expect("failed to read line");
893873
894874
let guess: u32 = match guess.trim().parse() {
@@ -920,7 +900,6 @@ let guess: u32 = match guess.trim().parse() {
920900
```
921901
922902
This is how you generally move from ‘crash on error’ to ‘actually handle the
923-
error’, by switching from `ok().expect()` to a `match` statement. The `Result`
924903
returned by `parse()` is an `enum` just like `Ordering`, but in this case, each
925904
variant has some data associated with it: `Ok` is a success, and `Err` is a
926905
failure. Each contains more information: the successfully parsed integer, or an
@@ -977,7 +956,6 @@ fn main() {
977956
let mut guess = String::new();
978957
979958
io::stdin().read_line(&mut guess)
980-
.ok()
981959
.expect("failed to read line");
982960
983961
let guess: u32 = match guess.trim().parse() {

0 commit comments

Comments
 (0)