Skip to content
Update rust error authored by umaumax's avatar umaumax
......@@ -351,11 +351,16 @@ Err("hoge").map_err(|e| MyError::InternalError(e.to_string()))?;
```
### anyhowで`Result<(), Box<dyn std::error::Error>>`の型を返す方法
例えば、main関数で`panic!`する代わりに利用した
``` rust
use anyhow::Context;
None.with_context(|| format!("hoge error"))?;
return None.with_context(|| format!("hoge error")).map_err(|e| e.into());
```
`Result<_, anyhow::Error>``?`によって、自動的に`Result<(), Box<dyn std::error::Error>>`へ変換される模様
これよりももっと簡潔な方法がありそうなのだが...
......
......