Closed13
Rustlings: if 編
何となくハンズオン形式の学習コンテンツは記事ではなくスクラップの方が良さげなので,if 編から試験的にスクラップを運用してみる.
if1.rs
Debugging ⚠️ Compiling of exercises/if/if1.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
--> exercises/if/if1.rs:5:34
|
5 | pub fn bigger(a: i32, b: i32) -> i32 {
| ------ ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
急に趣向が変わった:
./exercises/if/if1.rs より抜粋
pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
// Execute `rustlings hint if1` for hints
}
穴埋め問題か.単に完成されてないからビルドエラーが出るケース.
外部関数の呼び出しと新しい変数宣言は禁止,がレギュレーションか.
何も考えずに
pub fn bigger(a: i32, b: i32) -> i32 {
if (a > b) {
return a;
} else {
return b;
}
}
でいったら通った.ちなみに Rust には三項演算子(ternary operator)はないそう.
if2.rs
Debugging ⚠️ Compiling of exercises/if/if2.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
--> exercises/if/if2.rs:13:9
|
13 | 1
| ^ expected `&str`, found integer
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
とりあえずコードを読む.
単に if else の条件分岐で異なる型を出し分けているだけ,なので &str に揃える.
⚠️ Testing of exercises/if/if2.rs failed! Please try again. Here's the output:
running 3 tests
test tests::foo_for_fizz ... ok
test tests::bar_for_fuzz ... FAILED
test tests::default_to_baz ... FAILED
successes:
successes:
tests::foo_for_fizz
failures:
---- tests::bar_for_fuzz stdout ----
thread 'tests::bar_for_fuzz' panicked at 'assertion failed: `(left == right)`
left: `"1"`,
right: `"bar"`', exercises/if/if2.rs:29:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- tests::default_to_baz stdout ----
thread 'tests::default_to_baz' panicked at 'assertion failed: `(left == right)`
left: `"1"`,
right: `"baz"`', exercises/if/if2.rs:34:9
failures:
tests::bar_for_fuzz
tests::default_to_baz
test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
初めてテスト結果らしきものを見た気がする.
bar_for_fuzz
および default_to_baz
のアサーションでコケてそうなので丁寧に眺める.
テストコードは
#[test]
fn bar_for_fuzz() {
assert_eq!(fizz_if_foo("fuzz"), "bar")
}
#[test]
fn default_to_baz() {
assert_eq!(fizz_if_foo("literally anything"), "baz")
}
なので,これに合わせて実装を変更.
いけた,特に Rust らしき要素はゼロ.
次回予告: quit 編に突入
⚠️ Compiling of exercises/quiz1.rs failed! Please try again. Here's the output:
error[E0425]: cannot find function `calculate_apple_price` in this scope
--> exercises/quiz1.rs:18:18
|
18 | let price1 = calculate_apple_price(35);
| ^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `calculate_apple_price` in this scope
--> exercises/quiz1.rs:19:18
|
19 | let price2 = calculate_apple_price(40);
| ^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `calculate_apple_price` in this scope
--> exercises/quiz1.rs:20:18
|
20 | let price3 = calculate_apple_price(65);
| ^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0425`.
何かめっちゃ出てるけど見掛け倒しっぽいな.
このスクラップは2022/03/25にクローズされました