99// except according to those terms.
1010
1111//! Traits for working with Errors.
12- //!
13- //! # The `Error` trait
14- //!
15- //! `Error` is a trait representing the basic expectations for error values,
16- //! i.e. values of type `E` in [`Result<T, E>`]. At a minimum, errors must provide
17- //! a description, but they may optionally provide additional detail (via
18- //! [`Display`]) and cause chain information:
19- //!
20- //! ```
21- //! use std::fmt::Display;
22- //!
23- //! trait Error: Display {
24- //! fn description(&self) -> &str;
25- //!
26- //! fn cause(&self) -> Option<&Error> { None }
27- //! }
28- //! ```
29- //!
30- //! The [`cause`] method is generally used when errors cross "abstraction
31- //! boundaries", i.e. when a one module must report an error that is "caused"
32- //! by an error from a lower-level module. This setup makes it possible for the
33- //! high-level module to provide its own errors that do not commit to any
34- //! particular implementation, but also reveal some of its implementation for
35- //! debugging via [`cause`] chains.
36- //!
37- //! [`Result<T, E>`]: ../result/enum.Result.html
38- //! [`Display`]: ../fmt/trait.Display.html
39- //! [`cause`]: trait.Error.html#method.cause
4012
4113#![ stable( feature = "rust1" , since = "1.0.0" ) ]
4214
@@ -63,33 +35,48 @@ use num;
6335use str;
6436use string;
6537
66- /// Base functionality for all errors in Rust.
38+ /// `Error` is a trait representing the basic expectations for error values,
39+ /// i.e. values of type `E` in [`Result<T, E>`]. Errors must describe
40+ /// themselves through the [`Display`] and [`Debug`] traits, and may provide
41+ /// cause chain information:
42+ ///
43+ /// The [`cause`] method is generally used when errors cross "abstraction
44+ /// boundaries", i.e. when a one module must report an error that is "caused"
45+ /// by an error from a lower-level module. This setup makes it possible for the
46+ /// high-level module to provide its own errors that do not commit to any
47+ /// particular implementation, but also reveal some of its implementation for
48+ /// debugging via [`cause`] chains.
49+ ///
50+ /// [`Result<T, E>`]: ../result/enum.Result.html
51+ /// [`Display`]: ../fmt/trait.Display.html
52+ /// [`cause`]: trait.Error.html#method.cause
6753#[ stable( feature = "rust1" , since = "1.0.0" ) ]
6854pub trait Error : Debug + Display {
69- /// A short description of the error.
55+ /// **This method is soft-deprecated.**
7056 ///
71- /// The description should only be used for a simple message.
72- /// It should not contain newlines or sentence-ending punctuation,
73- /// to facilitate embedding in larger user-facing strings .
74- /// For showing formatted error messages with more information see
75- /// [`Display`] .
57+ /// Although using it won’t cause compilation warning,
58+ /// new code should use [`Display`] instead
59+ /// and new `impl`s can omit it .
60+ ///
61+ /// To obtain error description as a string, use `to_string()` .
7662 ///
7763 /// [`Display`]: ../fmt/trait.Display.html
7864 ///
7965 /// # Examples
8066 ///
8167 /// ```
82- /// use std::error::Error;
83- ///
8468 /// match "xc".parse::<u32>() {
8569 /// Err(e) => {
86- /// println!("Error: {}", e.description());
70+ /// // Print `e` itself, not `e.description()`.
71+ /// println!("Error: {}", e);
8772 /// }
8873 /// _ => println!("No error"),
8974 /// }
9075 /// ```
9176 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
92- fn description ( & self ) -> & str ;
77+ fn description ( & self ) -> & str {
78+ "description() is deprecated; use Display"
79+ }
9380
9481 /// The lower-level cause of this error, if any.
9582 ///
0 commit comments