-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sotanengel/main
問題文の追加
- Loading branch information
Showing
10 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...s/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."), | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...r_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 !"), | ||
}; | ||
} |
15 changes: 15 additions & 0 deletions
15
...refer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
16 changes: 16 additions & 0 deletions
16
...er_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...s/3_types_Prefer_option_and_result_transforms_over_explicit_match_expressions/1_if_let.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...r_option_and_result_transforms_over_explicit_match_expressions/2_processing_outsourced.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
16 changes: 16 additions & 0 deletions
16
...refer_option_and_result_transforms_over_explicit_match_expressions/3_unuse_result_type.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...er_option_and_result_transforms_over_explicit_match_expressions/4_sugar_result_process.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |