回答編集履歴

3

`anyhow::Error`の説明を修正

2020/06/27 18:03

投稿

qryxip
qryxip

スコア86

test CHANGED
@@ -128,7 +128,7 @@
128
128
 
129
129
 
130
130
 
131
- 現在(特に小規模なアプリケーションでは)エラー型に[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)の内容も表示してくれます。
131
+ 現在(特に小規模なアプリケーションでは)エラー型に[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)の内容も表示してくれます。
132
132
 
133
133
 
134
134
 

2

`Result::unwrap_or_else`は引数を一つ取る && `.ok().expect(_)`はClippyのwarn対象

2020/06/27 18:03

投稿

qryxip
qryxip

スコア86

test CHANGED
@@ -54,7 +54,11 @@
54
54
 
55
55
 
56
56
 
57
- (追記) `ParseIntError`を無視するなら`.expect("メッセージ")`のかわりに`.ok().expect("メッセージ")`か、`.unwrap_or_else(|| panic!("メッセージ"))`とすれば良いです。
57
+ (追記1) `ParseIntError`を無視するなら`.expect("メッセージ")`のかわりに`.ok().expect("メッセージ")`か、`.unwrap_or_else(|_| panic!("メッセージ"))`とすれば良いです。
58
+
59
+
60
+
61
+ (追記2) [.ok().expect(_)はClippy (Rustツールチェインに付いてくるlint)のwarning対象のようです。](https://rust-lang.github.io/rust-clippy/stable/index.html#ok_expect)
58
62
 
59
63
 
60
64
 
@@ -128,7 +132,7 @@
128
132
 
129
133
 
130
134
 
131
- (追記) このコードを貼るのを忘れてました...
135
+ (追記1) このコードを貼るのを忘れてました...
132
136
 
133
137
 
134
138
 

1

`ParseIntError`を無視する方法 && 貼り忘れてたコードを貼った

2020/06/27 17:55

投稿

qryxip
qryxip

スコア86

test CHANGED
@@ -51,6 +51,10 @@
51
51
  Panic message
52
52
 
53
53
  ```
54
+
55
+
56
+
57
+ (追記) `ParseIntError`を無視するなら`.expect("メッセージ")`のかわりに`.ok().expect("メッセージ")`か、`.unwrap_or_else(|| panic!("メッセージ"))`とすれば良いです。
54
58
 
55
59
 
56
60
 
@@ -124,6 +128,34 @@
124
128
 
125
129
 
126
130
 
131
+ (追記) このコードを貼るのを忘れてました...
132
+
133
+
134
+
135
+ ```rust
136
+
137
+ use anyhow::Context as _;
138
+
139
+ use std::fs;
140
+
141
+
142
+
143
+ fn main() -> anyhow::Result<()> {
144
+
145
+ let _content = fs::read_to_string("./nonexisting-file")
146
+
147
+ .with_context(|| "Failed to read ./nonexisting-file")?;
148
+
149
+
150
+
151
+ Ok(())
152
+
153
+ }
154
+
155
+ ```
156
+
157
+
158
+
127
159
  ```text
128
160
 
129
161
  $ cargo add anyhow