回答編集履歴
4
実行結果を追記しました
test
CHANGED
@@ -181,3 +181,35 @@
|
|
181
181
|
}
|
182
182
|
|
183
183
|
```
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
**実行結果**
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
# 正しいコマンドライン引数を与えた
|
194
|
+
|
195
|
+
$ cargo run -- 123
|
196
|
+
|
197
|
+
124
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
# 引数を与えなかった
|
202
|
+
|
203
|
+
$ cargo run --
|
204
|
+
|
205
|
+
Error: StringError("Please specify arg1.")
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# 数値に変換できない値を与えた
|
210
|
+
|
211
|
+
$ cargo run -- abc
|
212
|
+
|
213
|
+
Error: ParseIntError { kind: InvalidDigit }
|
214
|
+
|
215
|
+
```
|
3
最後の例にエラー時のテストケースを追加しました
test
CHANGED
@@ -172,6 +172,12 @@
|
|
172
172
|
|
173
173
|
assert_eq!(add1(x).unwrap(), 8);
|
174
174
|
|
175
|
+
|
176
|
+
|
177
|
+
// i32に変換できない文字列を与えるとエラー
|
178
|
+
|
179
|
+
assert!(add1("abc").is_err());
|
180
|
+
|
175
181
|
}
|
176
182
|
|
177
183
|
```
|
2
軽微な修正
test
CHANGED
@@ -66,7 +66,7 @@
|
|
66
66
|
|
67
67
|
// なるので便利
|
68
68
|
|
69
|
-
fn add1(s: &str) -> i32{
|
69
|
+
fn add1(s: &str) -> i32 {
|
70
70
|
|
71
71
|
let a = s.parse::<i32>().unwrap();
|
72
72
|
|
@@ -138,7 +138,7 @@
|
|
138
138
|
|
139
139
|
// String型
|
140
140
|
|
141
|
-
let arg1 = env::args().nth(1).ok_or
|
141
|
+
let arg1 = env::args().nth(1).ok_or("Please specify arg1.")?;
|
142
142
|
|
143
143
|
// &String → &strへ型強制する
|
144
144
|
|
1
`nth()`の例のテストケースを修正しました
test
CHANGED
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
#[test]
|
80
80
|
|
81
|
-
fn test_add1(){
|
81
|
+
fn test_add1() {
|
82
82
|
|
83
83
|
let x = "5".to_string(); // String型
|
84
84
|
|
@@ -162,11 +162,15 @@
|
|
162
162
|
|
163
163
|
#[test]
|
164
164
|
|
165
|
-
fn test_add1(){
|
165
|
+
fn test_add1() {
|
166
166
|
|
167
|
-
let x
|
167
|
+
let x = "5".to_string(); // String型
|
168
168
|
|
169
|
-
assert_eq!(add1(&x), 6);
|
169
|
+
assert_eq!(add1(&x).unwrap(), 6);
|
170
|
+
|
171
|
+
let x = "7"; // &str型
|
172
|
+
|
173
|
+
assert_eq!(add1(x).unwrap(), 8);
|
170
174
|
|
171
175
|
}
|
172
176
|
|