mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:24:32 -06:00
fix more links in error handling
This commit is contained in:
@@ -59,7 +59,7 @@ handling is reducing the amount of explicit case analysis the programmer has to
|
|||||||
do while keeping code composable.
|
do while keeping code composable.
|
||||||
|
|
||||||
Keeping code composable is important, because without that requirement, we
|
Keeping code composable is important, because without that requirement, we
|
||||||
could [`panic`](../std/macro.panic.html) whenever we
|
could [`panic`](../../std/macro.panic.html) whenever we
|
||||||
come across something unexpected. (`panic` causes the current task to unwind,
|
come across something unexpected. (`panic` causes the current task to unwind,
|
||||||
and in most cases, the entire program aborts.) Here's an example:
|
and in most cases, the entire program aborts.) Here's an example:
|
||||||
|
|
||||||
@@ -236,7 +236,7 @@ fn extension_explicit(file_name: &str) -> Option<&str> {
|
|||||||
```
|
```
|
||||||
|
|
||||||
(Pro-tip: don't use this code. Use the
|
(Pro-tip: don't use this code. Use the
|
||||||
[`extension`](../std/path/struct.Path.html#method.extension)
|
[`extension`](../../std/path/struct.Path.html#method.extension)
|
||||||
method in the standard library instead.)
|
method in the standard library instead.)
|
||||||
|
|
||||||
The code stays simple, but the important thing to notice is that the type of
|
The code stays simple, but the important thing to notice is that the type of
|
||||||
@@ -530,10 +530,10 @@ above is generic over all the different number types defined in the
|
|||||||
standard library. We could (and probably should) also make our
|
standard library. We could (and probably should) also make our
|
||||||
function generic, but let's favor explicitness for the moment. We only
|
function generic, but let's favor explicitness for the moment. We only
|
||||||
care about `i32`, so we need to [find its implementation of
|
care about `i32`, so we need to [find its implementation of
|
||||||
`FromStr`](../std/primitive.i32.html) (do a `CTRL-F` in your browser
|
`FromStr`](../../std/primitive.i32.html) (do a `CTRL-F` in your browser
|
||||||
for “FromStr”) and look at its [associated type][10] `Err`. We did
|
for “FromStr”) and look at its [associated type][10] `Err`. We did
|
||||||
this so we can find the concrete error type. In this case, it's
|
this so we can find the concrete error type. In this case, it's
|
||||||
[`std::num::ParseIntError`](../std/num/struct.ParseIntError.html).
|
[`std::num::ParseIntError`](../../std/num/struct.ParseIntError.html).
|
||||||
Finally, we can rewrite our function:
|
Finally, we can rewrite our function:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@@ -577,12 +577,12 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
The usual suspects are all there for `Result`, including
|
The usual suspects are all there for `Result`, including
|
||||||
[`unwrap_or`](../std/result/enum.Result.html#method.unwrap_or) and
|
[`unwrap_or`](../../std/result/enum.Result.html#method.unwrap_or) and
|
||||||
[`and_then`](../std/result/enum.Result.html#method.and_then).
|
[`and_then`](../../std/result/enum.Result.html#method.and_then).
|
||||||
Additionally, since `Result` has a second type parameter, there are
|
Additionally, since `Result` has a second type parameter, there are
|
||||||
combinators that affect only the error type, such as
|
combinators that affect only the error type, such as
|
||||||
[`map_err`](../std/result/enum.Result.html#method.map_err) (instead of
|
[`map_err`](../../std/result/enum.Result.html#method.map_err) (instead of
|
||||||
`map`) and [`or_else`](../std/result/enum.Result.html#method.or_else)
|
`map`) and [`or_else`](../../std/result/enum.Result.html#method.or_else)
|
||||||
(instead of `and_then`).
|
(instead of `and_then`).
|
||||||
|
|
||||||
### The `Result` type alias idiom
|
### The `Result` type alias idiom
|
||||||
@@ -611,11 +611,11 @@ Why would we do this? Well, if we have a lot of functions that could return
|
|||||||
uses `ParseIntError` so that we don't have to write it out all the time.
|
uses `ParseIntError` so that we don't have to write it out all the time.
|
||||||
|
|
||||||
The most prominent place this idiom is used in the standard library is
|
The most prominent place this idiom is used in the standard library is
|
||||||
with [`io::Result`](../std/io/type.Result.html). Typically, one writes
|
with [`io::Result`](../../std/io/type.Result.html). Typically, one writes
|
||||||
`io::Result<T>`, which makes it clear that you're using the `io`
|
`io::Result<T>`, which makes it clear that you're using the `io`
|
||||||
module's type alias instead of the plain definition from
|
module's type alias instead of the plain definition from
|
||||||
`std::result`. (This idiom is also used for
|
`std::result`. (This idiom is also used for
|
||||||
[`fmt::Result`](../std/fmt/type.Result.html).)
|
[`fmt::Result`](../../std/fmt/type.Result.html).)
|
||||||
|
|
||||||
## A brief interlude: unwrapping isn't evil
|
## A brief interlude: unwrapping isn't evil
|
||||||
|
|
||||||
@@ -639,7 +639,7 @@ summarize some of my *opinions* on the matter.
|
|||||||
|
|
||||||
This is probably not an exhaustive list. Moreover, when using an
|
This is probably not an exhaustive list. Moreover, when using an
|
||||||
`Option`, it is often better to use its
|
`Option`, it is often better to use its
|
||||||
[`expect`](../std/option/enum.Option.html#method.expect)
|
[`expect`](../../std/option/enum.Option.html#method.expect)
|
||||||
method. `expect` does exactly the same thing as `unwrap`, except it
|
method. `expect` does exactly the same thing as `unwrap`, except it
|
||||||
prints a message you give to `expect`. This makes the resulting panic
|
prints a message you give to `expect`. This makes the resulting panic
|
||||||
a bit nicer to deal with, since it will show your message instead of
|
a bit nicer to deal with, since it will show your message instead of
|
||||||
@@ -719,7 +719,7 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
There are a couple new things in this example. The first is the use of the
|
There are a couple new things in this example. The first is the use of the
|
||||||
[`Option::ok_or`](../std/option/enum.Option.html#method.ok_or)
|
[`Option::ok_or`](../../std/option/enum.Option.html#method.ok_or)
|
||||||
combinator. This is one way to convert an `Option` into a `Result`. The
|
combinator. This is one way to convert an `Option` into a `Result`. The
|
||||||
conversion requires you to specify what error to use if `Option` is `None`.
|
conversion requires you to specify what error to use if `Option` is `None`.
|
||||||
Like the other combinators we've seen, its definition is very simple:
|
Like the other combinators we've seen, its definition is very simple:
|
||||||
@@ -734,7 +734,7 @@ fn ok_or<T, E>(option: Option<T>, err: E) -> Result<T, E> {
|
|||||||
```
|
```
|
||||||
|
|
||||||
The other new combinator used here is
|
The other new combinator used here is
|
||||||
[`Result::map_err`](../std/result/enum.Result.html#method.map_err).
|
[`Result::map_err`](../../std/result/enum.Result.html#method.map_err).
|
||||||
This is like `Result::map`, except it maps a function on to the *error*
|
This is like `Result::map`, except it maps a function on to the *error*
|
||||||
portion of a `Result` value. If the `Result` is an `Ok(...)` value, then it is
|
portion of a `Result` value. If the `Result` is an `Ok(...)` value, then it is
|
||||||
returned unmodified.
|
returned unmodified.
|
||||||
@@ -781,7 +781,7 @@ fn main() {
|
|||||||
|
|
||||||
(N.B. The `AsRef<Path>` is used because those are the
|
(N.B. The `AsRef<Path>` is used because those are the
|
||||||
[same bounds used on
|
[same bounds used on
|
||||||
`std::fs::File::open`](../std/fs/struct.File.html#method.open).
|
`std::fs::File::open`](../../std/fs/struct.File.html#method.open).
|
||||||
This makes it ergonomic to use any kind of string as a file path.)
|
This makes it ergonomic to use any kind of string as a file path.)
|
||||||
|
|
||||||
There are three different errors that can occur here:
|
There are three different errors that can occur here:
|
||||||
@@ -791,16 +791,16 @@ There are three different errors that can occur here:
|
|||||||
3. A problem parsing the data as a number.
|
3. A problem parsing the data as a number.
|
||||||
|
|
||||||
The first two problems are described via the
|
The first two problems are described via the
|
||||||
[`std::io::Error`](../std/io/struct.Error.html) type. We know this
|
[`std::io::Error`](../../std/io/struct.Error.html) type. We know this
|
||||||
because of the return types of
|
because of the return types of
|
||||||
[`std::fs::File::open`](../std/fs/struct.File.html#method.open) and
|
[`std::fs::File::open`](../../std/fs/struct.File.html#method.open) and
|
||||||
[`std::io::Read::read_to_string`](../std/io/trait.Read.html#method.read_to_string).
|
[`std::io::Read::read_to_string`](../../std/io/trait.Read.html#method.read_to_string).
|
||||||
(Note that they both use the [`Result` type alias
|
(Note that they both use the [`Result` type alias
|
||||||
idiom](#the-result-type-alias-idiom) described previously. If you
|
idiom](#the-result-type-alias-idiom) described previously. If you
|
||||||
click on the `Result` type, you'll [see the type
|
click on the `Result` type, you'll [see the type
|
||||||
alias](../std/io/type.Result.html), and consequently, the underlying
|
alias](../../std/io/type.Result.html), and consequently, the underlying
|
||||||
`io::Error` type.) The third problem is described by the
|
`io::Error` type.) The third problem is described by the
|
||||||
[`std::num::ParseIntError`](../std/num/struct.ParseIntError.html)
|
[`std::num::ParseIntError`](../../std/num/struct.ParseIntError.html)
|
||||||
type. The `io::Error` type in particular is *pervasive* throughout the
|
type. The `io::Error` type in particular is *pervasive* throughout the
|
||||||
standard library. You will see it again and again.
|
standard library. You will see it again and again.
|
||||||
|
|
||||||
@@ -944,7 +944,7 @@ macro_rules! try {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
(The [real definition](../std/macro.try.html) is a bit more
|
(The [real definition](../../std/macro.try.html) is a bit more
|
||||||
sophisticated. We will address that later.)
|
sophisticated. We will address that later.)
|
||||||
|
|
||||||
Using the `try!` macro makes it very easy to simplify our last example. Since
|
Using the `try!` macro makes it very easy to simplify our last example. Since
|
||||||
@@ -1003,7 +1003,7 @@ determine the type of error is not robust. (Admittedly, this downside is far
|
|||||||
more important inside of a library as opposed to, say, an application.)
|
more important inside of a library as opposed to, say, an application.)
|
||||||
|
|
||||||
For example, the `io::Error` type embeds an
|
For example, the `io::Error` type embeds an
|
||||||
[`io::ErrorKind`](../std/io/enum.ErrorKind.html),
|
[`io::ErrorKind`](../../std/io/enum.ErrorKind.html),
|
||||||
which is *structured data* that represents what went wrong during an IO
|
which is *structured data* that represents what went wrong during an IO
|
||||||
operation. This is important because you might want to react differently
|
operation. This is important because you might want to react differently
|
||||||
depending on the error. (e.g., A `BrokenPipe` error might mean quitting your
|
depending on the error. (e.g., A `BrokenPipe` error might mean quitting your
|
||||||
@@ -1076,8 +1076,8 @@ that you don't remove choices from the caller unnecessarily.
|
|||||||
# Standard library traits used for error handling
|
# Standard library traits used for error handling
|
||||||
|
|
||||||
The standard library defines two integral traits for error handling:
|
The standard library defines two integral traits for error handling:
|
||||||
[`std::error::Error`](../std/error/trait.Error.html) and
|
[`std::error::Error`](../../std/error/trait.Error.html) and
|
||||||
[`std::convert::From`](../std/convert/trait.From.html). While `Error`
|
[`std::convert::From`](../../std/convert/trait.From.html). While `Error`
|
||||||
is designed specifically for generically describing errors, the `From`
|
is designed specifically for generically describing errors, the `From`
|
||||||
trait serves a more general role for converting values between two
|
trait serves a more general role for converting values between two
|
||||||
distinct types.
|
distinct types.
|
||||||
@@ -1085,7 +1085,7 @@ distinct types.
|
|||||||
## The `Error` trait
|
## The `Error` trait
|
||||||
|
|
||||||
The `Error` trait is [defined in the standard
|
The `Error` trait is [defined in the standard
|
||||||
library](../std/error/trait.Error.html):
|
library](../../std/error/trait.Error.html):
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use std::fmt::{Debug, Display};
|
use std::fmt::{Debug, Display};
|
||||||
@@ -1113,7 +1113,7 @@ The first two are a result of `Error` requiring impls for both `Debug` and
|
|||||||
`Display`. The latter two are from the two methods defined on `Error`. The
|
`Display`. The latter two are from the two methods defined on `Error`. The
|
||||||
power of `Error` comes from the fact that all error types impl `Error`, which
|
power of `Error` comes from the fact that all error types impl `Error`, which
|
||||||
means errors can be existentially quantified as a
|
means errors can be existentially quantified as a
|
||||||
[trait object](../book/first-edition/trait-objects.html).
|
[trait object](/trait-objects.html).
|
||||||
This manifests as either `Box<Error>` or `&Error`. Indeed, the `cause` method
|
This manifests as either `Box<Error>` or `&Error`. Indeed, the `cause` method
|
||||||
returns an `&Error`, which is itself a trait object. We'll revisit the
|
returns an `&Error`, which is itself a trait object. We'll revisit the
|
||||||
`Error` trait's utility as a trait object later.
|
`Error` trait's utility as a trait object later.
|
||||||
@@ -2196,7 +2196,7 @@ heuristics!
|
|||||||
found a healthy mix of `try!` and combinators to be quite appealing.
|
found a healthy mix of `try!` and combinators to be quite appealing.
|
||||||
`and_then`, `map` and `unwrap_or` are my favorites.
|
`and_then`, `map` and `unwrap_or` are my favorites.
|
||||||
|
|
||||||
[1]: ../book/first-edition/patterns.html
|
[1]: patterns.html
|
||||||
[2]: ../../std/option/enum.Option.html#method.map
|
[2]: ../../std/option/enum.Option.html#method.map
|
||||||
[3]: ../../std/option/enum.Option.html#method.unwrap_or
|
[3]: ../../std/option/enum.Option.html#method.unwrap_or
|
||||||
[4]: ../../std/option/enum.Option.html#method.unwrap_or_else
|
[4]: ../../std/option/enum.Option.html#method.unwrap_or_else
|
||||||
@@ -2205,7 +2205,7 @@ heuristics!
|
|||||||
[7]: ../../std/result/enum.Result.html#method.unwrap
|
[7]: ../../std/result/enum.Result.html#method.unwrap
|
||||||
[8]: ../../std/fmt/trait.Debug.html
|
[8]: ../../std/fmt/trait.Debug.html
|
||||||
[9]: ../../std/primitive.str.html#method.parse
|
[9]: ../../std/primitive.str.html#method.parse
|
||||||
[10]: ../book/first-edition/associated-types.html
|
[10]: associated-types.html
|
||||||
[11]: https://github.com/petewarden/dstkdata
|
[11]: https://github.com/petewarden/dstkdata
|
||||||
[12]: http://burntsushi.net/stuff/worldcitiespop.csv.gz
|
[12]: http://burntsushi.net/stuff/worldcitiespop.csv.gz
|
||||||
[13]: http://burntsushi.net/stuff/uscitiespop.csv.gz
|
[13]: http://burntsushi.net/stuff/uscitiespop.csv.gz
|
||||||
|
|||||||
Reference in New Issue
Block a user