@@ -4,6 +4,7 @@ An alternative to boxing errors is to wrap them in your own error type.
44
55``` rust,editable
66use std::error;
7+ use std::error::Error as _;
78use std::num::ParseIntError;
89use std::fmt;
910
@@ -22,8 +23,10 @@ impl fmt::Display for DoubleError {
2223 match *self {
2324 DoubleError::EmptyVec =>
2425 write!(f, "please use a vector with at least one element"),
25- // This is a wrapper, so defer to the underlying types' implementation of `fmt`.
26- DoubleError::Parse(ref e) => e.fmt(f),
26+ // The wrapped error contains additional information and is available
27+ // via the source() method.
28+ DoubleError::Parse(..) =>
29+ write!(f, "the provided string could not be parsed as int"),
2730 }
2831 }
2932}
@@ -51,6 +54,8 @@ impl From<ParseIntError> for DoubleError {
5154
5255fn double_first(vec: Vec<&str>) -> Result<i32> {
5356 let first = vec.first().ok_or(DoubleError::EmptyVec)?;
57+ // Here we implicitly use the `ParseIntError` implementation of `From` (which
58+ // we defined above) in order to create a `DoubleError`.
5459 let parsed = first.parse::<i32>()?;
5560
5661 Ok(2 * parsed)
@@ -59,7 +64,12 @@ fn double_first(vec: Vec<&str>) -> Result<i32> {
5964fn print(result: Result<i32>) {
6065 match result {
6166 Ok(n) => println!("The first doubled is {}", n),
62- Err(e) => println!("Error: {}", e),
67+ Err(e) => {
68+ println!("Error: {}", e);
69+ if let Some(source) = e.source() {
70+ println!(" Caused by: {}", source);
71+ }
72+ },
6373 }
6474}
6575
0 commit comments