January 2018
Beginner to intermediate
454 pages
10h 8m
English
Since we want to print the error to the terminal, we'll implement the Display trait for our Error type:
use std::fmt::{self, Display, Formatter}; use self::Error::*; impl Display for Error { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { match *self { FromUtf8(ref error) => error.fmt(formatter), Io(ref error) => error.fmt(formatter), Utf8(ref error) => error.fmt(formatter), Msg(ref msg) => write!(formatter, "{}", msg), } } }
For the three cases where we wrap an error from another type, we just call the corresponding fmt() method of these errors. In the case that it is a Msg, we write the string using the write! macro. This macro is a bit similar to print!, but needs a parameter to specify where to write the ...