@@ -2,8 +2,8 @@
|
||||
|
||||
## Error reporting
|
||||
|
||||
In the previous exercise you had to destructure the `InvalidTitle` variant to extract the error message and
|
||||
pass it to the `panic!` macro.
|
||||
In the previous exercise you had to destructure the `InvalidTitle` variant to extract the error message and
|
||||
pass it to the `panic!` macro.\
|
||||
This is a (rudimentary) example of **error reporting**: transforming an error type into a representation that can be
|
||||
shown to a user, a service operator, or a developer.
|
||||
|
||||
@@ -13,7 +13,7 @@ That's why Rust provides the `std::error::Error` trait.
|
||||
|
||||
## The `Error` trait
|
||||
|
||||
There are no constraints on the type of the `Err` variant in a `Result`, but it's a good practice to use a type
|
||||
There are no constraints on the type of the `Err` variant in a `Result`, but it's a good practice to use a type
|
||||
that implements the `Error` trait.
|
||||
`Error` is the cornerstone of Rust's error handling story:
|
||||
|
||||
@@ -31,7 +31,7 @@ implement `Debug` and `Display`.
|
||||
We've already encountered the `Debug` trait in [a previous exercise](../04_traits/04_derive.md)—it's the trait used by
|
||||
`assert_eq!` to display the values of the variables it's comparing when the assertion fails.
|
||||
|
||||
From a "mechanical" perspective, `Display` and `Debug` are identical—they encode how a type should be converted
|
||||
From a "mechanical" perspective, `Display` and `Debug` are identical—they encode how a type should be converted
|
||||
into a string-like representation:
|
||||
|
||||
```rust
|
||||
@@ -46,8 +46,8 @@ pub trait Display {
|
||||
}
|
||||
```
|
||||
|
||||
The difference is in their *purpose*: `Display` returns a representation that's meant for "end-users",
|
||||
while `Debug` provides a low-level representation that's more suitable to developers and service operators.
|
||||
The difference is in their _purpose_: `Display` returns a representation that's meant for "end-users",
|
||||
while `Debug` provides a low-level representation that's more suitable to developers and service operators.\
|
||||
That's why `Debug` can be automatically implemented using the `#[derive(Debug)]` attribute, while `Display`
|
||||
**requires** a manual implementation.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user