回答編集履歴
3
`anyhow::Error`の説明を修正
answer
CHANGED
@@ -63,7 +63,7 @@
|
|
63
63
|
|
64
64
|
(`Result<(), String>`や`Resutl<() &'static str>`とした場合、`Error: "エラーメッセージ"`のように`""`付きで表示されてしまいます。)
|
65
65
|
|
66
|
-
現在(特に小規模なアプリケーションでは)エラー型に[anyhow::Error](https://docs.rs/anyhow/1/anyhow/struct.Error.html)を使うことが推奨されています。 `anyhow::Error`は`Debug`ではなく[Display](https://doc.rust-lang.org/stable/std/fmt/trait.Display.html)で
|
66
|
+
現在(特に小規模なアプリケーションでは)エラー型に[anyhow::Error](https://docs.rs/anyhow/1/anyhow/struct.Error.html)を使うことが推奨されています。 `anyhow::Error`の`Debug`表示では中身のエラーは`Debug`ではなく[Display](https://doc.rust-lang.org/stable/std/fmt/trait.Display.html)で表示されます。 また[std::error::Error::source](https://doc.rust-lang.org/stable/std/error/trait.Error.html#method.source)の内容も表示してくれます。
|
67
67
|
|
68
68
|
(追記1) このコードを貼るのを忘れてました...
|
69
69
|
|
2
`Result::unwrap_or_else`は引数を一つ取る && `.ok().expect(_)`はClippyのwarn対象
answer
CHANGED
@@ -26,8 +26,10 @@
|
|
26
26
|
Panic message
|
27
27
|
```
|
28
28
|
|
29
|
-
(追記) `ParseIntError`を無視するなら`.expect("メッセージ")`のかわりに`.ok().expect("メッセージ")`か、`.unwrap_or_else(|| panic!("メッセージ"))`とすれば良いです。
|
29
|
+
(追記1) `ParseIntError`を無視するなら`.expect("メッセージ")`のかわりに`.ok().expect("メッセージ")`か、`.unwrap_or_else(|_| panic!("メッセージ"))`とすれば良いです。
|
30
30
|
|
31
|
+
(追記2) [.ok().expect(_)はClippy (Rustツールチェインに付いてくるlint)のwarning対象のようです。](https://rust-lang.github.io/rust-clippy/stable/index.html#ok_expect)
|
32
|
+
|
31
33
|
ところで[前の質問](https://teratail.com/questions/273369?rss)でのコードでも触れましたが、`main`の返り値には`()`の他に`Result<(), E>` (`E: Debug`)が使えます。 こちらを使うことをおすすめします。
|
32
34
|
|
33
35
|
`main`の返り値を`Result`して実際に`Err`が返される場合、`Error: {Debug表記}`というエラーメッセージになります。
|
@@ -63,7 +65,7 @@
|
|
63
65
|
|
64
66
|
現在(特に小規模なアプリケーションでは)エラー型に[anyhow::Error](https://docs.rs/anyhow/1/anyhow/struct.Error.html)を使うことが推奨されています。 `anyhow::Error`は`Debug`ではなく[Display](https://doc.rust-lang.org/stable/std/fmt/trait.Display.html)でエラーを表示します。 また[std::error::Error::source](https://doc.rust-lang.org/stable/std/error/trait.Error.html#method.source)の内容も表示してくれます。
|
65
67
|
|
66
|
-
(追記) このコードを貼るのを忘れてました...
|
68
|
+
(追記1) このコードを貼るのを忘れてました...
|
67
69
|
|
68
70
|
```rust
|
69
71
|
use anyhow::Context as _;
|
1
`ParseIntError`を無視する方法 && 貼り忘れてたコードを貼った
answer
CHANGED
@@ -26,6 +26,8 @@
|
|
26
26
|
Panic message
|
27
27
|
```
|
28
28
|
|
29
|
+
(追記) `ParseIntError`を無視するなら`.expect("メッセージ")`のかわりに`.ok().expect("メッセージ")`か、`.unwrap_or_else(|| panic!("メッセージ"))`とすれば良いです。
|
30
|
+
|
29
31
|
ところで[前の質問](https://teratail.com/questions/273369?rss)でのコードでも触れましたが、`main`の返り値には`()`の他に`Result<(), E>` (`E: Debug`)が使えます。 こちらを使うことをおすすめします。
|
30
32
|
|
31
33
|
`main`の返り値を`Result`して実際に`Err`が返される場合、`Error: {Debug表記}`というエラーメッセージになります。
|
@@ -61,6 +63,20 @@
|
|
61
63
|
|
62
64
|
現在(特に小規模なアプリケーションでは)エラー型に[anyhow::Error](https://docs.rs/anyhow/1/anyhow/struct.Error.html)を使うことが推奨されています。 `anyhow::Error`は`Debug`ではなく[Display](https://doc.rust-lang.org/stable/std/fmt/trait.Display.html)でエラーを表示します。 また[std::error::Error::source](https://doc.rust-lang.org/stable/std/error/trait.Error.html#method.source)の内容も表示してくれます。
|
63
65
|
|
66
|
+
(追記) このコードを貼るのを忘れてました...
|
67
|
+
|
68
|
+
```rust
|
69
|
+
use anyhow::Context as _;
|
70
|
+
use std::fs;
|
71
|
+
|
72
|
+
fn main() -> anyhow::Result<()> {
|
73
|
+
let _content = fs::read_to_string("./nonexisting-file")
|
74
|
+
.with_context(|| "Failed to read ./nonexisting-file")?;
|
75
|
+
|
76
|
+
Ok(())
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
64
80
|
```text
|
65
81
|
$ cargo add anyhow
|
66
82
|
Updating 'https://github.com/rust-lang/crates.io-index' index
|