定义一个要求为智能指针的 rust error 错误
#[derive(Debug)]
pub struct GetFileError {
details: String,
}
impl GetFileError {
pub fn new(msg: String) -> GetFileError {
GetFileError { details: msg }
}
}
impl fmt::Display for GetFileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
}
}
impl std::error::Error for GetFileError {
fn description(&self) -> &str {
&self.details
}
}
调用
pub fn error_method() -> Box<dyn std::error::Error> {
return Err(GetFileError::new("Test File Error").to_owned())
}