On this page
  1. Error
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

Error

Prev: `Display` | Up: error_and_fmt | Next: `format`
Rel: error_and_fmt, rust/builtins
Ref: Rust std — Error
Tags: #public #prog #rust #stdlib #error_and_fmt #tier/core


Signature & Overview

trait Error: Debug + Display { fn source(&self) -> Option<&(dyn Error + 'static)> }

Standard trait for composable error types (Box<dyn Error> or custom error enums).

Notes & Pitfalls

Example

use std::{error::Error, fmt};

#[derive(Debug)]
struct ParseError(&'static str);

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) }
}
impl Error for ParseError {}

fn main() {
    let err: Box<dyn Error> = Box::new(ParseError("empty input"));
    assert_eq!(err.to_string(), "empty input");
}

Linked from

Related notes