Skip to content

Commit

Permalink
Updated usage of StrExt.parse() as per a recommendation by edwardw.
Browse files Browse the repository at this point in the history
  • Loading branch information
genbattle committed Feb 12, 2015
1 parent 9e9b1d6 commit 8300095
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/doc/reference.md
Expand Up @@ -3005,7 +3005,7 @@ Some examples of call expressions:
# fn add(x: i32, y: i32) -> i32 { 0 }
let x: i32 = add(1i32, 2i32);
let pi: Option<f32> = "3.14".parse();
let pi: Result<f32, _> = "3.14".parse();
```

### Lambda expressions
Expand Down
46 changes: 23 additions & 23 deletions src/doc/trpl/guessing-game.md
Expand Up @@ -400,7 +400,7 @@ a function for that:
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.parse().ok();
let input_num: Result<u32, _> = input.parse();
```
The `parse` function takes in a `&str` value and converts it into something.
Expand All @@ -422,8 +422,8 @@ 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>().ok(); // input_num: Option<u32>
let input_num: Option<u32> = "5".parse().ok(); // input_num: Option<u32>
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>
```
Here we're converting the `Result` returned by `parse` to an `Option` by using
Expand All @@ -447,9 +447,9 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.parse().ok();
let input_num: Result<u32, _> = input.parse();
println!("You guessed: {}", input_num);
println!("You guessed: {:?}", input_num);
match cmp(input_num, secret_number) {
Ordering::Less => println!("Too small!"),
Expand Down Expand Up @@ -497,11 +497,11 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.parse().ok();
let input_num: Result<u32, _> = input.parse();

let num = match input_num {
Some(num) => num,
None => {
Ok(num) => num,
Err(_) => {
println!("Please input a number!");
return;
}
Expand Down Expand Up @@ -564,11 +564,11 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.trim().parse().ok();
let input_num: Result<u32, _> = input.trim().parse();

let num = match input_num {
Some(num) => num,
None => {
Ok(num) => num,
Err(_) => {
println!("Please input a number!");
return;
}
Expand Down Expand Up @@ -640,11 +640,11 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.trim().parse().ok();
let input_num: Result<u32, _> = input.trim().parse();
let num = match input_num {
Some(num) => num,
None => {
Ok(num) => num,
Err(_) => {
println!("Please input a number!");
return;
}
Expand Down Expand Up @@ -716,11 +716,11 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.trim().parse().ok();
let input_num: Result<u32, _> = input.trim().parse();

let num = match input_num {
Some(num) => num,
None => {
Ok(num) => num,
Err(_) => {
println!("Please input a number!");
return;
}
Expand Down Expand Up @@ -772,11 +772,11 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.trim().parse().ok();
let input_num: Result<u32, _> = input.trim().parse();

let num = match input_num {
Some(num) => num,
None => {
Ok(num) => num,
Err(_) => {
println!("Please input a number!");
continue;
}
Expand Down Expand Up @@ -849,11 +849,11 @@ fn main() {
let input = old_io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<u32> = input.trim().parse().ok();
let input_num: Result<u32, _> = input.trim().parse();

let num = match input_num {
Some(num) => num,
None => {
Ok(num) => num,
Err(_) => {
println!("Please input a number!");
continue;
}
Expand Down

0 comments on commit 8300095

Please sign in to comment.