forked from rust-lang/rust-by-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated and restructured Ch16.5, 16.6
Readability and grammar changes Renamed "Result as an alias" to "aliases for Result" Made "map for Result" and "aliases for Result" into children of "Result"
- Loading branch information
1 parent
82d29fb
commit 659c6cf
Showing
8 changed files
with
75 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::num::ParseIntError; | ||
use std::result; | ||
|
||
// Define a generic alias for a `Result` of type `ParseIntError`. | ||
type AliasedResult<T> = result::Result<T, ParseIntError>; | ||
|
||
// Use the alias defined above to refer to our specific `Result` type. | ||
fn double_number(number_str: &str) -> AliasedResult<i32> { | ||
number_str.parse::<i32>().map(|n| 2 * n) | ||
} | ||
|
||
// Here, the alias again allows us to save some space. | ||
fn print(result: AliasedResult<i32>) { | ||
match result { | ||
Ok(n) => println!("n is {}", n), | ||
Err(e) => println!("Error: {}", e), | ||
} | ||
} | ||
|
||
fn main() { | ||
print(double_number("10")); | ||
print(double_number("t")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
How about when we want to reuse a specific `Result` type many times? | ||
It quickly becomes tedious to write out the full type name, but recall that Rust allows | ||
us to create [aliases][alias]. A generic alias may conveniently be defined for the | ||
specific `Result` in question: | ||
|
||
{alias.play} | ||
|
||
At a module level, creating aliases can be particularly helpful. Errors | ||
found in a specific module often have the same `Err` type, so a single alias can succinctly | ||
define *all* associated `Results`. This is so useful that the `std` library even supplies one: `io::Result`! | ||
|
||
### See also: | ||
|
||
[`Result`][result] and [`io::Result`][io_result] | ||
|
||
[alias]: /cast/alias.html | ||
[result]: http://doc.rust-lang.org/std/result/enum.Result.html | ||
[io_result]: http://doc.rust-lang.org/std/io/type.Result.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Panicking on `unwrap()` in the previous example gave us an unhelpful error message. | ||
To avoid that, we need to be more specific about the return type. In that example, | ||
recall that the regular element is of type `i32`. To determine the `Err` type, we | ||
look to `parse()`. `parse()` is implemented with the [`FromStr trait`][from_str] | ||
for [`i32`][i32]. As a result, the `Err` type is specified as [`ParseIntError`][parse_int_error]. | ||
|
||
In the example below, note that using the straightforward `match` statement leads to | ||
more cumbersome code. As it turns out, the `map` method we used with `Option` | ||
is also implemented for `Result`. | ||
|
||
{result.play} | ||
|
||
Much like `Option`, `Result` implements combinators besides `map`, such as `and_then` | ||
and `unwrap_or`. This even includes those that specifically handle errors, like `map_err`. | ||
[`Result`][result] contains the complete listing. | ||
|
||
### See also: | ||
|
||
[`i32`][i32], [`FromStr`][from_str], and [`ParseIntErr`][parse_int_error] | ||
|
||
[result]: http://doc.rust-lang.org/std/result/enum.Result.html | ||
[parse_int_error]: http://doc.rust-lang.org/std/num/struct.ParseIntError.html | ||
[from_str]: http://doc.rust-lang.org/std/str/trait.FromStr.html | ||
[i32]: http://doc.rust-lang.org/std/primitive.i32.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters