How to implement std error Error trait

Implement the std::error::Error trait by defining the source method on your custom error type.

Implement the std::error::Error trait on your custom error type by adding the impl block and defining the source method.

use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct MyError {
    msg: String,
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl Error for MyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}