Skip to content

Commit f396a31

Browse files
committed
Add an example for std::error::Error
1 parent 500a686 commit f396a31

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/core/src/error.rs

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ use crate::fmt::{self, Debug, Display, Formatter};
2222
/// accessing that error via [`Error::source()`]. This makes it possible for the
2323
/// high-level module to provide its own errors while also revealing some of the
2424
/// implementation for debugging.
25+
///
26+
/// # Example
27+
///
28+
/// Implementing the `Error` trait only requires that `Debug` and `Display` are implemented too.
29+
///
30+
/// ```
31+
/// use std::error::Error;
32+
/// use std::fmt;
33+
/// use std::path::PathBuf;
34+
///
35+
/// #[derive(Debug)]
36+
/// struct ReadConfigError {
37+
/// path: PathBuf
38+
/// }
39+
///
40+
/// impl fmt::Display for ReadConfigError {
41+
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42+
/// let path = self.path.display();
43+
/// write!(f, "unable to read configuration at {path}")
44+
/// }
45+
/// }
46+
///
47+
/// impl Error for ReadConfigError {}
48+
/// ```
2549
#[stable(feature = "rust1", since = "1.0.0")]
2650
#[cfg_attr(not(test), rustc_diagnostic_item = "Error")]
2751
#[rustc_has_incoherent_inherent_impls]

0 commit comments

Comments
 (0)