@@ -400,7 +400,7 @@ a function for that:
400400let input = old_io::stdin().read_line ()
401401 .ok ()
402402 .expect(" Failed to read line" );
403- let input_num: Option< u32> = input.parse ();
403+ let input_num: Option< u32> = input.parse().ok () ;
404404` ` `
405405
406406The ` parse` function takes in a ` & str` value and converts it into something.
@@ -422,11 +422,13 @@ In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
422422tell ` 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: Option< u32> = " 5" .parse(); // input_num: Option< u32>
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>
427427` ` `
428428
429- Anyway, with us now converting our input to a number, our code looks like this:
429+ Here we' re converting the `Result` returned by `parse` to an `Option` by using
430+ the `ok` method as well. Anyway, with us now converting our input to a number,
431+ our code looks like this:
430432
431433```{rust,ignore}
432434use std::old_io;
@@ -445,7 +447,7 @@ fn main() {
445447 let input = old_io::stdin().read_line()
446448 .ok()
447449 .expect("Failed to read line");
448- let input_num: Option< u32> = input.parse ();
450+ let input_num: Option<u32> = input.parse().ok() ;
449451
450452 println!("You guessed: {}", input_num);
451453
@@ -495,7 +497,7 @@ fn main() {
495497 let input = old_io::stdin().read_line ()
496498 .ok ()
497499 .expect(" Failed to read line" );
498- let input_num: Option<u32> = input.parse();
500+ let input_num: Option< u32> = input.parse().ok () ;
499501
500502 let num = match input_num {
501503 Some(num) => num,
@@ -562,7 +564,7 @@ fn main() {
562564 let input = old_io::stdin().read_line ()
563565 .ok ()
564566 .expect(" Failed to read line" );
565- let input_num: Option<u32> = input.trim().parse();
567+ let input_num: Option< u32> = input.trim().parse().ok () ;
566568
567569 let num = match input_num {
568570 Some(num) => num,
@@ -638,7 +640,7 @@ fn main() {
638640 let input = old_io::stdin().read_line()
639641 .ok()
640642 .expect("Failed to read line");
641- let input_num: Option< u32> = input.trim().parse ();
643+ let input_num: Option<u32> = input.trim().parse().ok() ;
642644
643645 let num = match input_num {
644646 Some(num) => num,
@@ -714,7 +716,7 @@ fn main() {
714716 let input = old_io::stdin().read_line ()
715717 .ok ()
716718 .expect(" Failed to read line" );
717- let input_num: Option<u32> = input.trim().parse();
719+ let input_num: Option< u32> = input.trim().parse().ok () ;
718720
719721 let num = match input_num {
720722 Some(num) => num,
@@ -770,7 +772,7 @@ fn main() {
770772 let input = old_io::stdin().read_line ()
771773 .ok ()
772774 .expect(" Failed to read line" );
773- let input_num: Option<u32> = input.trim().parse();
775+ let input_num: Option< u32> = input.trim().parse().ok () ;
774776
775777 let num = match input_num {
776778 Some(num) => num,
@@ -847,7 +849,7 @@ fn main() {
847849 let input = old_io::stdin().read_line ()
848850 .ok ()
849851 .expect(" Failed to read line" );
850- let input_num: Option<u32> = input.trim().parse();
852+ let input_num: Option< u32> = input.trim().parse().ok () ;
851853
852854 let num = match input_num {
853855 Some(num) => num,
0 commit comments