Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Changes
Page history
Update rust error
authored
Dec 03, 2021
by
umaumax
Show whitespace changes
Inline
Side-by-side
rust/rust-error.md
View page @
f27461fb
...
...
@@ -101,6 +101,27 @@ pub enum MyError {
}
```
### thiserrorでよくあるクレートのエラー型の自動変換用の定義例
```
rust
pub
type
Result
<
T
,
E
=
Error
>
=
std
::
result
::
Result
<
T
,
E
>
;
#[derive(Error,
Debug)]
pub
enum
Error
{
#[error(transparent)]
IO
(
#[from]
std
::
io
::
Error
),
#[error(transparent)]
Anyhow
(
#[from]
anyhow
::
Error
),
#[error(transparent)]
SerdeYaml
(
#[from]
serde_yaml
::
Error
),
#[error(transparent)]
SerdeJson
(
#[from]
serde_json
::
Error
),
#[error(transparent)]
Image
(
#[from]
image
::
ImageError
),
#[error(transparent)]
CSV
(
#[from]
csv
::
Error
),
}
```
### `anyhow::Error`から`thiserror`への移行方法
方針
*
`anyhow::Result`
や
`Box<dyn std::error::Error>`
を
`pub type Result<T, E = MyError> = std::result::Result<T, E>;`
へ置き換える
...
...
...
...