October 2019
Intermediate to advanced
444 pages
10h 37m
English
Defining error types is not hard—just follow these steps:
use std::fmt;use std::error::Error;#[derive(Debug)]pub struct MyError { code: usize,}
impl Error for MyError { fn description(&self) -> &str { "Occurs when someone makes a mistake" }}
impl fmt::Display for MyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Error code {:#X}", self.code) }}
Read now
Unlock full access