diff --git a/Cargo.toml b/Cargo.toml index 92b9a0c..27d00c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,14 @@ bin = [ { name = "2_function_pointer_sol", path = "solutions/2_types_Use_the_type_system_to_express_common_behavior/2_function_pointer.rs" }, { name = "3_closure", path = "exercises/2_types_Use_the_type_system_to_express_common_behavior/3_closure.rs" }, { name = "3_closure_sol", path = "solutions/2_types_Use_the_type_system_to_express_common_behavior/3_closure.rs" }, + { name = "1_if_let", path = "exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs" }, + { name = "1_if_let_sol", path = "solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs" }, + { name = "2_processing_outsourced", path = "exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs" }, + { name = "2_processing_outsourced_sol", path = "solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs" }, + { name = "3_unuse_result_type", path = "exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs" }, + { name = "3_unuse_result_type_sol", path = "solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs" }, + { name = "4_sugar_result_process", path = "exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs" }, + { name = "4_sugar_result_process_sol", path = "solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs" }, ] [package] diff --git a/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs new file mode 100644 index 0000000..43021de --- /dev/null +++ b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs @@ -0,0 +1,9 @@ +fn main() { + let value: Option = Some(42); + + // TODO: Noneの場合の処理は必要ないのでifを使った処理に変更してください。 + match value { + Some(x) => println!("Found: {}", x), + None => println!("No value found."), + } +} diff --git a/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs new file mode 100644 index 0000000..8dbcd33 --- /dev/null +++ b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs @@ -0,0 +1,9 @@ +fn main() { + let value: Option = Some(42); + + // TODO: expectを使って冗長な処理を簡潔にしてください。 + match value { + Some(num) => println!("The value is: {}", num), + None => panic!("Something wrong !"), + }; +} diff --git a/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs new file mode 100644 index 0000000..30fba4f --- /dev/null +++ b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs @@ -0,0 +1,15 @@ +fn divide(a: i32, b: i32) -> Result { + if b == 0 { + Err("Division by zero is not allowed") + } else { + Ok(a / b) + } +} + +fn main() { + let result = divide(10, 0); + + // TODO: 返却されたResult型は使用する必要があります。 + // matchを使って、計算結果を表示してください。 + println!("result is Error"); +} diff --git a/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs new file mode 100644 index 0000000..ee607ce --- /dev/null +++ b/exercises/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs @@ -0,0 +1,16 @@ +fn divide(a: i32, b: i32) -> Result { + if b == 0 { + Err("Division by zero is not allowed") + } else { + Ok(a / b) + } +} + +fn main() { + let result = divide(10, 3); // TODO: クエスションマークを使ってさらに簡潔に表現しましょう。 + + match result { + Ok(value) => println!("Result: {}", value), + Err(e) => panic!("Result: {}", e), + } +} diff --git a/info.toml b/info.toml index c2bc75c..d74e5bd 100644 --- a/info.toml +++ b/info.toml @@ -60,3 +60,31 @@ dir = "2_types_Use_the_type_system_to_express_common_behavior" test = false hint = """クロージャの基本を学びます。環境をキャプチャする関数を理解しましょう。""" skip_check_unsolved = false + +[[exercises]] +name = "1_if_let" +dir = "3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions" +test = false +hint = """if let構文を使って、OptionやResultの安全なunwrapを学びます。""" +skip_check_unsolved = true + +[[exercises]] +name = "2_processing_outsourced" +dir = "3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions" +test = false +hint = """処理を他の関数に委譲する方法を学びます。""" +skip_check_unsolved = true + +[[exercises]] +name = "3_unuse_result_type" +dir = "3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions" +test = false +hint = """Result型の未使用を防ぐ方法を学びます。""" +skip_check_unsolved = true + +[[exercises]] +name = "4_sugar_result_process" +dir = "3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions" +test = false +hint = """Result型のシュガー表現を使って、エラーハンドリングを簡潔に記述します。""" +skip_check_unsolved = true diff --git a/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs new file mode 100644 index 0000000..d776a68 --- /dev/null +++ b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs @@ -0,0 +1,8 @@ +fn main() { + let some_value: Option = Some(42); + + // 値がある場合だけ処理を行う + if let Some(x) = some_value { + println!("Found: {}", x); + } +} diff --git a/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs new file mode 100644 index 0000000..ab3abdb --- /dev/null +++ b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs @@ -0,0 +1,7 @@ +#[allow(clippy::unnecessary_literal_unwrap)] +fn main() { + let value: Option = Some(42); + + let result = value.expect("Something wrong !"); + println!("The value is: {}", result); +} diff --git a/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs new file mode 100644 index 0000000..9946f1b --- /dev/null +++ b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs @@ -0,0 +1,16 @@ +fn divide(a: i32, b: i32) -> Result { + if b == 0 { + Err("Division by zero is not allowed") + } else { + Ok(a / b) + } +} + +fn main() { + let result = divide(10, 0); + + match result { + Ok(value) => println!("Result: {}", value), + Err(e) => println!("Result: {}", e), + } +} diff --git a/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs new file mode 100644 index 0000000..2d976f4 --- /dev/null +++ b/solutions/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs @@ -0,0 +1,14 @@ +fn divide(a: i32, b: i32) -> Result { + if b == 0 { + Err("Division by zero is not allowed") + } else { + Ok(a / b) + } +} + +fn main() -> Result<(), &'static str> { + let value = divide(10, 3)?; // クエスションマークでエラーハンドリング + println!("Result: {}", value); + + Ok(()) +}