Skip to content

Guessing Game: Changed wording to use Result instead of Option in several places, fixed... #22911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/doc/trpl/guessing-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,11 @@ In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
tell `random()` what to generate. In a similar fashion, both of these work:

```{rust,ignore}
let input_num = "5".parse::<u32>(); // input_num: Option<u32>
let input_num: Result<u32, _> = "5".parse(); // input_num: Result<u32, <u32 as FromStr>::Err>
let input_num_option = "5".parse::<u32>().ok(); // input_num: Option<u32>
let input_num_result: Result<u32, _> = "5".parse(); // input_num: Result<u32, <u32 as FromStr>::Err>
```

Here we're converting the `Result` returned by `parse` to an `Option` by using
Above, we're converting the `Result` returned by `parse` to an `Option` by using
the `ok` method as well. Anyway, with us now converting our input to a number,
our code looks like this:

Expand Down Expand Up @@ -470,14 +470,14 @@ Let's try it out!
```bash
$ cargo build
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
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)
src/main.rs:22 match cmp(input_num, secret_number) {
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]
src/main.rs:21 match cmp(input_num, secret_number) {
^~~~~~~~~
error: aborting due to previous error
```

Oh yeah! Our `input_num` has the type `Option<u32>`, rather than `u32`. We
need to unwrap the Option. If you remember from before, `match` is a great way
Oh yeah! Our `input_num` has the type `Result<u32, <some error>>`, rather than `u32`. We
need to unwrap the Result. If you remember from before, `match` is a great way
to do that. Try this code:

```{rust,no_run}
Expand All @@ -500,7 +500,7 @@ fn main() {
let input_num: Result<u32, _> = input.parse();

let num = match input_num {
Ok(num) => num,
Ok(n) => n,
Err(_) => {
println!("Please input a number!");
return;
Expand All @@ -524,7 +524,7 @@ fn cmp(a: u32, b: u32) -> Ordering {
}
```

We use a `match` to either give us the `u32` inside of the `Option`, or else
We use a `match` to either give us the `u32` inside of the `Result`, or else
print an error message and return. Let's give this a shot:

```bash
Expand Down