-
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.
- Loading branch information
1 parent
374ca90
commit 597d403
Showing
10 changed files
with
209 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
12 changes: 12 additions & 0 deletions
12
exercises/9_types_Consider_using_iterator_transforms_instead_of_explicit_loops/1_iter_sum.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,12 @@ | ||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let numbers = vec![1, 2, 3, 4, 5]; | ||
|
||
// TODO: イテレータを使って簡潔に実装し直してください。 | ||
let mut sum = 0; | ||
for i in 0..numbers.len() { | ||
sum += numbers[i]; | ||
} | ||
|
||
println!("Sum: {}", sum); | ||
} |
15 changes: 15 additions & 0 deletions
15
...cises/9_types_Consider_using_iterator_transforms_instead_of_explicit_loops/2_iter_skip.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 @@ | ||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | ||
|
||
let mut sum = 0; | ||
let mut index = 2; // 最初の2要素をスキップ | ||
|
||
// TODO: イテレータを使って簡潔に実装し直してください。 | ||
while index < numbers.len() { | ||
sum += numbers[index]; | ||
index += 3; // 3つおきに要素を取得 | ||
} | ||
|
||
println!("Sum of elements at multiples of 3 indices: {}", sum); | ||
} |
16 changes: 16 additions & 0 deletions
16
...ses/9_types_Consider_using_iterator_transforms_instead_of_explicit_loops/3_iter_filter.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 @@ | ||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let numbers = vec![17, 3, 25, 8, 42, 7, 19, 6, 1, 12, 9, 5]; | ||
|
||
let mut odd_numbers = Vec::new(); | ||
|
||
// TODO: イテレータを使って簡潔に実装し直してください。 | ||
for i in 0..numbers.len() { | ||
let x = numbers[i]; | ||
if x % 2 != 0 && x < 10 { | ||
odd_numbers.push(x); | ||
} | ||
} | ||
|
||
println!("{:?}", odd_numbers); | ||
} |
52 changes: 52 additions & 0 deletions
52
..._types_Consider_using_iterator_transforms_instead_of_explicit_loops/4_turbofish_syntax.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,52 @@ | ||
fn parse_numbers(items: Vec<&str>) -> Result<Vec<i32>, std::num::ParseIntError> { | ||
let mut results = Vec::new(); | ||
|
||
// TODO: イテレータを使って簡潔に実装し直してください。 | ||
for i in 0..items.len() { | ||
match items[i].parse::<i32>() { | ||
Ok(num) => results.push(num), | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
|
||
Ok(results) | ||
} | ||
|
||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let items = vec!["42", "93", "apple", "17"]; | ||
let parsed = parse_numbers(items); | ||
|
||
match parsed { | ||
Ok(numbers) => println!("Parsed numbers: {:?}", numbers), | ||
Err(e) => eprintln!("Failed to parse: {}", e), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_numbers_success() { | ||
let items = vec!["42", "93", "17"]; | ||
let result = parse_numbers(items); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), vec![42, 93, 17]); | ||
} | ||
|
||
#[test] | ||
fn test_parse_numbers_failure() { | ||
let items = vec!["42", "apple", "17"]; | ||
let result = parse_numbers(items); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_parse_empty_list() { | ||
let items: Vec<&str> = vec![]; | ||
let result = parse_numbers(items); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), vec![]); | ||
} | ||
} |
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
solutions/9_types_Consider_using_iterator_transforms_instead_of_explicit_loops/1_iter_sum.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 @@ | ||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let numbers = vec![1, 2, 3, 4, 5]; | ||
|
||
// イテレータを使って合計を計算します。 | ||
let sum: i32 = numbers.iter().sum(); | ||
|
||
println!("Sum: {}", sum); | ||
} |
12 changes: 12 additions & 0 deletions
12
...tions/9_types_Consider_using_iterator_transforms_instead_of_explicit_loops/2_iter_skip.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,12 @@ | ||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | ||
|
||
let sum: i32 = numbers | ||
.iter() | ||
.skip(2) // 最初の2要素をスキップ | ||
.step_by(3) // 3つおきに要素を取得 | ||
.sum(); | ||
|
||
println!("Sum of elements at multiples of 3 indices: {}", sum); | ||
} |
12 changes: 12 additions & 0 deletions
12
...ons/9_types_Consider_using_iterator_transforms_instead_of_explicit_loops/3_iter_filter.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,12 @@ | ||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let numbers = vec![17, 3, 25, 8, 42, 7, 19, 6, 1, 12, 9, 5]; | ||
|
||
let odd_numbers: Vec<i32> = numbers | ||
.iter() | ||
.filter(|&&x| x % 2 != 0 && x < 10) | ||
.copied() | ||
.collect(); | ||
|
||
println!("{:?}", odd_numbers); | ||
} |
45 changes: 45 additions & 0 deletions
45
..._types_Consider_using_iterator_transforms_instead_of_explicit_loops/4_turbofish_syntax.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,45 @@ | ||
fn parse_numbers(items: Vec<&str>) -> Result<Vec<i32>, std::num::ParseIntError> { | ||
items | ||
.into_iter() | ||
.map(|item| item.parse::<i32>()) | ||
.collect::<Result<Vec<_>, _>>() | ||
} | ||
|
||
#[allow(clippy::useless_vec)] | ||
fn main() { | ||
let items = vec!["42", "93", "apple", "17"]; | ||
let parsed = parse_numbers(items); | ||
|
||
match parsed { | ||
Ok(numbers) => println!("Parsed numbers: {:?}", numbers), | ||
Err(e) => eprintln!("Failed to parse: {}", e), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_numbers_success() { | ||
let items = vec!["42", "93", "17"]; | ||
let result = parse_numbers(items); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), vec![42, 93, 17]); | ||
} | ||
|
||
#[test] | ||
fn test_parse_numbers_failure() { | ||
let items = vec!["42", "apple", "17"]; | ||
let result = parse_numbers(items); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_parse_empty_list() { | ||
let items: Vec<&str> = vec![]; | ||
let result = parse_numbers(items); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), vec![]); | ||
} | ||
} |