trait

Error — standard error interface

Implement Debug for diagnostics and Display for user-facing text; Error can then provide additional information about causes. The default source is absent. A Result itself does not impose this trait on E. Expected stdout (not executed): quantity must be positive Common mistake: Implementing Error without its supertraits.

Formal Syntax & Structure

rust syntaxrust
pub trait Error: Debug + Display

Working Example

Example Snippetrust
use std::{error::Error, fmt};#[derive(Debug)]struct QuantityError;impl fmt::Display for QuantityError {    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "quantity must be positive") }}impl Error for QuantityError {}fn main() {    let e: &dyn Error = &QuantityError;    println!("{e}");}
Rust Reference