Skip to content

Commit 16efd0e

Browse files
committed
Rollup merge of #22911 - djmally:master, r=steveklabnik
... example that actually does use an Option
2 parents 973272e + a457dd5 commit 16efd0e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/doc/trpl/guessing-game.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
422422
tell `random()` what to generate. In a similar fashion, both of these work:
423423
424424
```{rust,ignore}
425-
let input_num = "5".parse::<u32>(); // input_num: Option<u32>
426-
let input_num: Result<u32, _> = "5".parse(); // input_num: Result<u32, <u32 as FromStr>::Err>
425+
let input_num_option = "5".parse::<u32>().ok(); // input_num: Option<u32>
426+
let input_num_result: Result<u32, _> = "5".parse(); // input_num: Result<u32, <u32 as FromStr>::Err>
427427
```
428428
429-
Here we're converting the `Result` returned by `parse` to an `Option` by using
429+
Above, we're converting the `Result` returned by `parse` to an `Option` by using
430430
the `ok` method as well. Anyway, with us now converting our input to a number,
431431
our code looks like this:
432432
@@ -470,14 +470,14 @@ Let's try it out!
470470
```bash
471471
$ cargo build
472472
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
473-
src/main.rs:22:15: 22:24 error: mismatched types: expected `u32` but found `core::option::Option<u32>` (expected u32 but found enum core::option::Option)
474-
src/main.rs:22 match cmp(input_num, secret_number) {
473+
src/main.rs:21:15: 21:24 error: mismatched types: expected `u32`, found `core::result::Result<u32, core::num::ParseIntError>` (expected u32, found enum `core::result::Result`) [E0308]
474+
src/main.rs:21 match cmp(input_num, secret_number) {
475475
^~~~~~~~~
476476
error: aborting due to previous error
477477
```
478478
479-
Oh yeah! Our `input_num` has the type `Option<u32>`, rather than `u32`. We
480-
need to unwrap the Option. If you remember from before, `match` is a great way
479+
Oh yeah! Our `input_num` has the type `Result<u32, <some error>>`, rather than `u32`. We
480+
need to unwrap the Result. If you remember from before, `match` is a great way
481481
to do that. Try this code:
482482
483483
```{rust,no_run}
@@ -500,7 +500,7 @@ fn main() {
500500
let input_num: Result<u32, _> = input.parse();
501501

502502
let num = match input_num {
503-
Ok(num) => num,
503+
Ok(n) => n,
504504
Err(_) => {
505505
println!("Please input a number!");
506506
return;
@@ -524,7 +524,7 @@ fn cmp(a: u32, b: u32) -> Ordering {
524524
}
525525
```
526526
527-
We use a `match` to either give us the `u32` inside of the `Option`, or else
527+
We use a `match` to either give us the `u32` inside of the `Result`, or else
528528
print an error message and return. Let's give this a shot:
529529
530530
```bash

0 commit comments

Comments
 (0)