Skip to content

Commit

Permalink
error: Add thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Mar 13, 2024
1 parent 1ddf80c commit 4656d46
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/error-handling/thiserror.md
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/)

0 comments on commit 4656d46

Please sign in to comment.