9
9
// except according to those terms.
10
10
11
11
//! 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
40
12
41
13
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
42
14
@@ -63,33 +35,48 @@ use num;
63
35
use str;
64
36
use string;
65
37
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
67
53
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
68
54
pub trait Error : Debug + Display {
69
- /// A short description of the error.
55
+ /// **This method is soft-deprecated.**
70
56
///
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()` .
76
62
///
77
63
/// [`Display`]: ../fmt/trait.Display.html
78
64
///
79
65
/// # Examples
80
66
///
81
67
/// ```
82
- /// use std::error::Error;
83
- ///
84
68
/// match "xc".parse::<u32>() {
85
69
/// Err(e) => {
86
- /// println!("Error: {}", e.description());
70
+ /// // Print `e` itself, not `e.description()`.
71
+ /// println!("Error: {}", e);
87
72
/// }
88
73
/// _ => println!("No error"),
89
74
/// }
90
75
/// ```
91
76
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
92
- fn description ( & self ) -> & str ;
77
+ fn description ( & self ) -> & str {
78
+ "description() is deprecated; use Display"
79
+ }
93
80
94
81
/// The lower-level cause of this error, if any.
95
82
///
0 commit comments