diff options
| author | Nick Sarten <gen.battle@gmail.com> | 2015-02-12 20:48:09 +1300 |
|---|---|---|
| committer | Nick Sarten <gen.battle@gmail.com> | 2015-02-12 20:48:09 +1300 |
| commit | 830009543d07b16e832283a175de644261ce75fb (patch) | |
| tree | 21fbf449348ebf27104a7212d85e7bfa520b05f6 /src/doc | |
| parent | 9e9b1d6085f6b14ff3992cd2104a29322e213dd2 (diff) | |
| download | rust-830009543d07b16e832283a175de644261ce75fb.tar.gz rust-830009543d07b16e832283a175de644261ce75fb.zip | |
Updated usage of StrExt.parse() as per a recommendation by edwardw.
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/reference.md | 2 | ||||
| -rw-r--r-- | src/doc/trpl/guessing-game.md | 46 |
2 files changed, 24 insertions, 24 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index e1463249207..73880549ae6 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<f32> = "3.14".parse(); +let pi: Result<f32, _> = "3.14".parse(); ``` ### Lambda expressions diff --git a/src/doc/trpl/guessing-game.md b/src/doc/trpl/guessing-game.md index 162e533d8bb..01f270f1951 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<u32> = input.parse().ok(); +let input_num: Result<u32, _> = 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::<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 @@ -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!"), @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } |
