Skip to content

Commit

Permalink
Merge pull request #3 from sotanengel/main
Browse files Browse the repository at this point in the history
問題文の追加
  • Loading branch information
sotanengel authored Jan 2, 2025
2 parents b3744e7 + 6d4c10a commit 312b460
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
let value: Option<i32> = Some(42);

// TODO: Noneの場合の処理は必要ないのでifを使った処理に変更してください。
match value {
Some(x) => println!("Found: {}", x),
None => println!("No value found."),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
let value: Option<i32> = Some(42);

// TODO: expectを使って冗長な処理を簡潔にしてください。
match value {
Some(num) => println!("The value is: {}", num),
None => panic!("Something wrong !"),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
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");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
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),
}
}
28 changes: 28 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let some_value: Option<i32> = Some(42);

// 値がある場合だけ処理を行う
if let Some(x) = some_value {
println!("Found: {}", x);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[allow(clippy::unnecessary_literal_unwrap)]
fn main() {
let value: Option<i32> = Some(42);

let result = value.expect("Something wrong !");
println!("The value is: {}", result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
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),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
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(())
}

0 comments on commit 312b460

Please sign in to comment.