You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- A `Display` impl is generated for your type if you provide doc comment
49
-
messages on the struct or each variant of your enum, as shown above in the
50
-
example.
51
-
52
-
The messages support a shorthand for interpolating fields from the error.
53
-
57
+
- A `fmt::Display` impl is generated for your enum if you provide
58
+
a docstring comment on each variant as shown above in the example. The
59
+
`Display` derive macro supports a shorthand for interpolating fields from
60
+
the error:
54
61
-`/// {var}` ⟶ `write!("{}", self.var)`
55
62
-`/// {0}` ⟶ `write!("{}", self.0)`
56
63
-`/// {var:?}` ⟶ `write!("{:?}", self.var)`
57
64
-`/// {0:?}` ⟶ `write!("{:?}", self.0)`
65
+
- This also works with structs and [generic types][crate::Display#generic-type-parameters]:
66
+
```rust
67
+
/// oh no, an error: {0}
68
+
#[derive(Display)]
69
+
pubstructError<E>(pubE);
70
+
71
+
leterror:Error<&str> =Error("muahaha i am an error");
72
+
assert!("oh no, an error: muahaha i am an error"==&format!("{}", error));
73
+
```
58
74
59
75
- Two optional attributes can be added to your types next to the derive:
60
76
@@ -77,10 +93,10 @@ pub enum DataStoreError {
77
93
### FAQ
78
94
79
95
1.**Is this crate `no_std` compatible?**
80
-
* Yes! This crate implements the `core::fmt::Display` trait not the `std::fmt::Display` trait so it should work in `std` and `no_std` environments. Just add `default-features = false`.
96
+
* Yes! This crate implements the [`core::fmt::Display`] trait, not the [`std::fmt::Display`] trait, so it should work in `std` and `no_std` environments. Just add `default-features = false`.
81
97
82
98
2.**Does this crate work with `Path` and `PathBuf` via the `Display` trait?**
83
-
* Yuuup. This crate uses @dtolnay's [autoref specialization technique](https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md) to add a special trait for types to get the display impl, it then specializes for `Path` and `PathBuf` and when either of these types are found it calls `self.display()` to get a `std::path::Display<'_>` type which can be used with the Display format specifier!
99
+
* Yuuup. This crate uses @dtolnay's [autoref specialization technique](https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md) to add a special trait for types to get the display impl. It then specializes for `Path` and `PathBuf`, and when either of these types are found, it calls `self.display()` to get a `std::path::Display<'_>` type which can be used with the `Display` format specifier!
0 commit comments