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 15, 2021
by
umaumax
Show whitespace changes
Inline
Side-by-side
rust/rust-error.md
View page @
032cac8b
...
...
@@ -135,8 +135,7 @@ pub enum Error {
*
既存のエラー型を受け入れられる
`thiserror`
のエラー型を
`Anyhow(#[from] anyhow::Error)`
のようにして作成する
*
`anyhow::bail!`
や
`anyhow::ensure!`
で
`thiserror`
のエラー型で返せるように自前定義のマクロに置き換える
## anyhow::Resultの使い方
## anyhow
### anyhow::Result<>は普通の構造体をエラーとして持つ場合は`?`を利用できる
ResultのEがErrorトレイトを満たす単なる構造体については
`?`
を利用するだけで良い
...
...
@@ -167,6 +166,9 @@ let box_error: Box<dyn std::error::Error + Send + Sync + 'static> = todo!();
let
anyhow_error
=
anyhow
::
Error
::
from
(
&
box_error
);
```
## eyre(anyhowと同じ役割)
[
yaahc/eyre: A trait object based error handling type for easy idiomatic error handling and reporting in Rust applications
](
https://github.com/yaahc/eyre
)
## ライブラリを利用しない場合の汎用的なエラー型
`Result<(), Box<dyn std::error::Error>`
を関数の返り値に利用すると良い
...
...
...
...