-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
# thiserror | ||
# thiserror 库 | ||
|
||
这个库用于快速给结构体或者枚举实现 `Error` trait. | ||
|
||
使用方法极为简单: | ||
|
||
```rust | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum DataStoreError { | ||
#[error("data store disconnected")] | ||
Disconnect(#[from] io::Error), | ||
#[error("the data for key `{0}` is not available")] | ||
Redaction(String), | ||
#[error("invalid header (expected {expected:?}, found {found:?})")] | ||
InvalidHeader { | ||
expected: String, | ||
found: String, | ||
}, | ||
#[error("unknown data store error")] | ||
Unknown, | ||
} | ||
``` | ||
|
||
它会根据宏定义, 自动为结构体实现 `Display` trait. | ||
|
||
## 参考 | ||
|
||
- [thiserror document](https://docs.rs/thiserror/latest/thiserror/) |