diff --git a/src/doc/reference.md b/src/doc/reference.md index e14632492070..73880549ae64 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -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 = "3.14".parse(); +let pi: Result = "3.14".parse(); ``` ### Lambda expressions diff --git a/src/doc/trpl/guessing-game.md b/src/doc/trpl/guessing-game.md index 162e533d8bb7..01f270f19512 100644 --- a/src/doc/trpl/guessing-game.md +++ b/src/doc/trpl/guessing-game.md @@ -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 = input.parse().ok(); +let input_num: Result = input.parse(); ``` 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 tell `random()` what to generate. In a similar fashion, both of these work: ```{rust,ignore} -let input_num = "5".parse::().ok(); // input_num: Option -let input_num: Option = "5".parse().ok(); // input_num: Option +let input_num = "5".parse::(); // input_num: Option +let input_num: Result = "5".parse(); // input_num: Result::Err> ``` Here we're converting the `Result` returned by `parse` to an `Option` by using @@ -447,9 +447,9 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.parse().ok(); + let input_num: Result = input.parse(); - println!("You guessed: {}", input_num); + println!("You guessed: {:?}", input_num); match cmp(input_num, secret_number) { Ordering::Less => println!("Too small!"), @@ -497,11 +497,11 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.parse().ok(); + let input_num: Result = input.parse(); let num = match input_num { - Some(num) => num, - None => { + Ok(num) => num, + Err(_) => { println!("Please input a number!"); return; } @@ -564,11 +564,11 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.trim().parse().ok(); + let input_num: Result = input.trim().parse(); let num = match input_num { - Some(num) => num, - None => { + Ok(num) => num, + Err(_) => { println!("Please input a number!"); return; } @@ -640,11 +640,11 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.trim().parse().ok(); + let input_num: Result = input.trim().parse(); let num = match input_num { - Some(num) => num, - None => { + Ok(num) => num, + Err(_) => { println!("Please input a number!"); return; } @@ -716,11 +716,11 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.trim().parse().ok(); + let input_num: Result = input.trim().parse(); let num = match input_num { - Some(num) => num, - None => { + Ok(num) => num, + Err(_) => { println!("Please input a number!"); return; } @@ -772,11 +772,11 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.trim().parse().ok(); + let input_num: Result = input.trim().parse(); let num = match input_num { - Some(num) => num, - None => { + Ok(num) => num, + Err(_) => { println!("Please input a number!"); continue; } @@ -849,11 +849,11 @@ fn main() { let input = old_io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.trim().parse().ok(); + let input_num: Result = input.trim().parse(); let num = match input_num { - Some(num) => num, - None => { + Ok(num) => num, + Err(_) => { println!("Please input a number!"); continue; }