about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChris Denton <chris@chrisdenton.dev>2025-02-16 07:49:22 +0000
committerChris Denton <chris@chrisdenton.dev>2025-02-16 08:14:41 +0000
commitf396a3107507bb1925eba5a429da6adc40a06d61 (patch)
tree87511fc427dddd32dc6d30bbe01713a0979940af
parent500a686ba8bb1b51df7e7f8f81d286b2e20209ff (diff)
downloadrust-f396a3107507bb1925eba5a429da6adc40a06d61.tar.gz
rust-f396a3107507bb1925eba5a429da6adc40a06d61.zip
Add an example for std::error::Error
-rw-r--r--library/core/src/error.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/library/core/src/error.rs b/library/core/src/error.rs
index 33cf2af30b9..69ad7239954 100644
--- a/library/core/src/error.rs
+++ b/library/core/src/error.rs
@@ -22,6 +22,30 @@ use crate::fmt::{self, Debug, Display, Formatter};
 /// accessing that error via [`Error::source()`]. This makes it possible for the
 /// high-level module to provide its own errors while also revealing some of the
 /// implementation for debugging.
+///
+/// # Example
+///
+/// Implementing the `Error` trait only requires that `Debug` and `Display` are implemented too.
+///
+/// ```
+/// use std::error::Error;
+/// use std::fmt;
+/// use std::path::PathBuf;
+///
+/// #[derive(Debug)]
+/// struct ReadConfigError {
+///     path: PathBuf
+/// }
+///
+/// impl fmt::Display for ReadConfigError {
+///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+///         let path = self.path.display();
+///         write!(f, "unable to read configuration at {path}")
+///     }
+/// }
+///
+/// impl Error for ReadConfigError {}
+/// ```
 #[stable(feature = "rust1", since = "1.0.0")]
 #[cfg_attr(not(test), rustc_diagnostic_item = "Error")]
 #[rustc_has_incoherent_inherent_impls]