Skip to content

Commit 75fc40c

Browse files
committed
Remove a gotcha from book/error-handling.md
The book's "Error handling with Box<Error>" section talks about Box<Error>. In the actual example Box<Error + Send + Sync> is used instead so that the corresponding From impls could be used to convert a plain string to an error type. Rust 1.7 added support for conversion from &str/String to Box<Error>, so this gotcha and later references to it can now be removed.
1 parent 763f923 commit 75fc40c

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/doc/book/error-handling.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ use std::error::Error;
18291829
18301830
fn search<P: AsRef<Path>>
18311831
(file_path: P, city: &str)
1832-
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
1832+
-> Result<Vec<PopulationCount>, Box<Error>> {
18331833
let mut found = vec![];
18341834
let file = try!(File::open(file_path));
18351835
let mut rdr = csv::Reader::from_reader(file);
@@ -1858,20 +1858,17 @@ Instead of `x.unwrap()`, we now have `try!(x)`. Since our function returns a
18581858
`Result<T, E>`, the `try!` macro will return early from the function if an
18591859
error occurs.
18601860

1861-
There is one big gotcha in this code: we used `Box<Error + Send + Sync>`
1862-
instead of `Box<Error>`. We did this so we could convert a plain string to an
1863-
error type. We need these extra bounds so that we can use the
1864-
[corresponding `From`
1865-
impls](../std/convert/trait.From.html):
1861+
At the end of `search` we also convert a plain string to an error type
1862+
by using the [corresponding `From` impls](../std/convert/trait.From.html):
18661863

18671864
```rust,ignore
18681865
// We are making use of this impl in the code above, since we call `From::from`
18691866
// on a `&'static str`.
1870-
impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a>
1867+
impl<'a> From<&'a str> for Box<Error>
18711868
18721869
// But this is also useful when you need to allocate a new string for an
18731870
// error message, usually with `format!`.
1874-
impl From<String> for Box<Error + Send + Sync>
1871+
impl From<String> for Box<Error>
18751872
```
18761873

18771874
Since `search` now returns a `Result<T, E>`, `main` should use case analysis
@@ -1964,7 +1961,7 @@ use std::io;
19641961
19651962
fn search<P: AsRef<Path>>
19661963
(file_path: &Option<P>, city: &str)
1967-
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
1964+
-> Result<Vec<PopulationCount>, Box<Error>> {
19681965
let mut found = vec![];
19691966
let input: Box<io::Read> = match *file_path {
19701967
None => Box::new(io::stdin()),
@@ -2175,9 +2172,8 @@ heuristics!
21752172
`unwrap`. Be warned: if it winds up in someone else's hands, don't be
21762173
surprised if they are agitated by poor error messages!
21772174
* If you're writing a quick 'n' dirty program and feel ashamed about panicking
2178-
anyway, then use either a `String` or a `Box<Error + Send + Sync>` for your
2179-
error type (the `Box<Error + Send + Sync>` type is because of the
2180-
[available `From` impls](../std/convert/trait.From.html)).
2175+
anyway, then use either a `String` or a `Box<Error>` for your
2176+
error type.
21812177
* Otherwise, in a program, define your own error types with appropriate
21822178
[`From`](../std/convert/trait.From.html)
21832179
and

0 commit comments

Comments
 (0)