derive(Display) /// From<docs>

Latest Version Rust Documentation

This library provides a convenient derive macro for the standard library's [core::fmt::Display] trait.

toml [dependencies] displaydoc = "0.2"

Compiler support: requires rustc 1.56+


Example

Demonstration alongside the [Error][std::error::Error] derive macro from thiserror, to propagate source locations from [io::Error][std::io::Error] with the #[source] attribute: ```rust use std::io; use displaydoc::Display; use thiserror::Error;

[derive(Display, Error, Debug)]

pub enum DataStoreError { /// data store disconnected Disconnect(#[source] io::Error), /// the data for key {0} is not available Redaction(String), /// invalid header (expected {expected:?}, found {found:?}) InvalidHeader { expected: String, found: String, }, /// unknown data store error Unknown, }

let error = DataStoreError::Redaction("CLASSIFIED CONTENT".to_string()); assert!("the data for key CLASSIFIED CONTENT is not available" == &format!("{}", error)); `` *Note that although [io::Error][std::io::Error] implementsDisplay, we do not add it to the generated message forDataStoreError::Disconnect, since it is already made available via #[source]`. See further context on avoiding duplication in error reports at the rust blog here.*


Details

pub struct Error(pub E);

let error: Error<&str> = Error("muahaha i am an error"); assert!("oh no, an error: muahaha i am an error" == &format!("{}", error)); ```


FAQ

  1. Is this crate no_std compatible?

  2. Does this crate work with Path and PathBuf via the Display trait?

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.