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
Mar 24, 2022
by
umaumax
Show whitespace changes
Inline
Side-by-side
rust/rust-error.md
View page @
def13901
...
@@ -351,11 +351,16 @@ Err("hoge").map_err(|e| MyError::InternalError(e.to_string()))?;
...
@@ -351,11 +351,16 @@ Err("hoge").map_err(|e| MyError::InternalError(e.to_string()))?;
```
```
### anyhowで`Result<(), Box<dyn std::error::Error>>`の型を返す方法
### anyhowで`Result<(), Box<dyn std::error::Error>>`の型を返す方法
例えば、main関数で
`panic!`
する代わりに利用した
```
rust
```
rust
use
anyhow
::
Context
;
use
anyhow
::
Context
;
None
.with_context
(||
format!
(
"hoge error"
))
?
;
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>>`
へ変換される模様
`Result<_, anyhow::Error>`
が
`?`
によって、自動的に
`Result<(), Box<dyn std::error::Error>>`
へ変換される模様
これよりももっと簡潔な方法がありそうなのだが...
これよりももっと簡潔な方法がありそうなのだが...
...
...
...
...