How to handle multiple error types

Define a custom enum listing all error variants and implement the Error trait to handle them uniformly.

Define a custom enum to list all possible error types and implement the Error trait to handle them uniformly.

use std::fmt;

enum MyError {
    Io(std::io::Error),
    Parse(std::num::ParseIntError),
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MyError::Io(e) => write!(f, "IO error: {}", e),
            MyError::Parse(e) => write!(f, "Parse error: {}", e),
        }
    }
}

impl std::error::Error for MyError {}

fn run() -> Result<(), MyError> {
    let io_err = std::io::Error::new(std::io::ErrorKind::Other, "oops");
    Err(MyError::Io(io_err))
}