Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace for loop with iteration #1404

Merged
merged 1 commit into from
Jan 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/std_misc/threads/testcase_mapreduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,13 @@ fn main() {
* Collect our intermediate results, and combine them into a final result
************************************************************************/

// collect each thread's intermediate results into a new Vec
let mut intermediate_sums = vec![];
for child in children {
// collect each child thread's return-value
let intermediate_sum = child.join().unwrap();
intermediate_sums.push(intermediate_sum);
}

// combine all intermediate sums into a single final sum.
// combine each thread's intermediate results into a single final sum.
//
// we use the "turbofish" ::<> to provide sum() with a type hint.
//
// TODO: try without the turbofish, by instead explicitly
// specifying the type of final_result
let final_result = intermediate_sums.iter().sum::<u32>();
let final_result = children.into_iter().map(|c| c.join().unwrap()).sum::<u32>();

println!("Final sum result: {}", final_result);
}
Expand Down