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
Oct 20, 2021
by
umaumax
Show whitespace changes
Inline
Side-by-side
rust/rust-error.md
View page @
432a4d5f
...
...
@@ -149,6 +149,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
> 戻り値の型は`Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>`とした方がベターとのことです.
### エラーごとに処理を分岐したい
[
Beginner's guide to Error Handling in Rust
](
https://www.sheshbabu.com/posts/rust-error-handling/
)
#### `Box<dyn std::error::Error>`に対して、`downcast_ref::<HogeError>()`を利用する
エラーの種類が多い場合や、1種類のみしか分岐しない場合などに利用しやすいが、ユーザとしては使いにくい
#### `enum`を利用してカスタムエラー型を作成する
[
Wrapping errors - Rust By Example
](
https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/wrap_error.html
)
エラーの種類を列挙するパターンなので、しっかりとしたエラーハンドリング向けであり、ユーザとしては使いやすい
## デバッグのために適当なエラーを利用したい
[
Error in std::io - Rust
](
https://doc.rust-lang.org/std/io/struct.Error.html
)
...
...
...
...