Skip to content

Commit 8300095

Browse files
committed
Updated usage of StrExt.parse() as per a recommendation by edwardw.
1 parent 9e9b1d6 commit 8300095

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/doc/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3005,7 +3005,7 @@ Some examples of call expressions:
30053005
# fn add(x: i32, y: i32) -> i32 { 0 }
30063006
30073007
let x: i32 = add(1i32, 2i32);
3008-
let pi: Option<f32> = "3.14".parse();
3008+
let pi: Result<f32, _> = "3.14".parse();
30093009
```
30103010

30113011
### Lambda expressions

src/doc/trpl/guessing-game.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ a function for that:
400400
let input = old_io::stdin().read_line()
401401
.ok()
402402
.expect("Failed to read line");
403-
let input_num: Option<u32> = input.parse().ok();
403+
let input_num: Result<u32, _> = input.parse();
404404
```
405405
406406
The `parse` function takes in a `&str` value and converts it into something.
@@ -422,8 +422,8 @@ 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>().ok(); // input_num: Option<u32>
426-
let input_num: Option<u32> = "5".parse().ok(); // input_num: Option<u32>
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>
427427
```
428428
429429
Here we're converting the `Result` returned by `parse` to an `Option` by using
@@ -447,9 +447,9 @@ fn main() {
447447
let input = old_io::stdin().read_line()
448448
.ok()
449449
.expect("Failed to read line");
450-
let input_num: Option<u32> = input.parse().ok();
450+
let input_num: Result<u32, _> = input.parse();
451451
452-
println!("You guessed: {}", input_num);
452+
println!("You guessed: {:?}", input_num);
453453
454454
match cmp(input_num, secret_number) {
455455
Ordering::Less => println!("Too small!"),
@@ -497,11 +497,11 @@ fn main() {
497497
let input = old_io::stdin().read_line()
498498
.ok()
499499
.expect("Failed to read line");
500-
let input_num: Option<u32> = input.parse().ok();
500+
let input_num: Result<u32, _> = input.parse();
501501

502502
let num = match input_num {
503-
Some(num) => num,
504-
None => {
503+
Ok(num) => num,
504+
Err(_) => {
505505
println!("Please input a number!");
506506
return;
507507
}
@@ -564,11 +564,11 @@ fn main() {
564564
let input = old_io::stdin().read_line()
565565
.ok()
566566
.expect("Failed to read line");
567-
let input_num: Option<u32> = input.trim().parse().ok();
567+
let input_num: Result<u32, _> = input.trim().parse();
568568

569569
let num = match input_num {
570-
Some(num) => num,
571-
None => {
570+
Ok(num) => num,
571+
Err(_) => {
572572
println!("Please input a number!");
573573
return;
574574
}
@@ -640,11 +640,11 @@ fn main() {
640640
let input = old_io::stdin().read_line()
641641
.ok()
642642
.expect("Failed to read line");
643-
let input_num: Option<u32> = input.trim().parse().ok();
643+
let input_num: Result<u32, _> = input.trim().parse();
644644
645645
let num = match input_num {
646-
Some(num) => num,
647-
None => {
646+
Ok(num) => num,
647+
Err(_) => {
648648
println!("Please input a number!");
649649
return;
650650
}
@@ -716,11 +716,11 @@ fn main() {
716716
let input = old_io::stdin().read_line()
717717
.ok()
718718
.expect("Failed to read line");
719-
let input_num: Option<u32> = input.trim().parse().ok();
719+
let input_num: Result<u32, _> = input.trim().parse();
720720

721721
let num = match input_num {
722-
Some(num) => num,
723-
None => {
722+
Ok(num) => num,
723+
Err(_) => {
724724
println!("Please input a number!");
725725
return;
726726
}
@@ -772,11 +772,11 @@ fn main() {
772772
let input = old_io::stdin().read_line()
773773
.ok()
774774
.expect("Failed to read line");
775-
let input_num: Option<u32> = input.trim().parse().ok();
775+
let input_num: Result<u32, _> = input.trim().parse();
776776

777777
let num = match input_num {
778-
Some(num) => num,
779-
None => {
778+
Ok(num) => num,
779+
Err(_) => {
780780
println!("Please input a number!");
781781
continue;
782782
}
@@ -849,11 +849,11 @@ fn main() {
849849
let input = old_io::stdin().read_line()
850850
.ok()
851851
.expect("Failed to read line");
852-
let input_num: Option<u32> = input.trim().parse().ok();
852+
let input_num: Result<u32, _> = input.trim().parse();
853853

854854
let num = match input_num {
855-
Some(num) => num,
856-
None => {
855+
Ok(num) => num,
856+
Err(_) => {
857857
println!("Please input a number!");
858858
continue;
859859
}

0 commit comments

Comments
 (0)