Skip to content

Commit 03e23af

Browse files
authored
Merge pull request #1404 from theidexisted/master
Replace for loop with iteration
2 parents 32e2dc8 + 4ff2dd8 commit 03e23af

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

src/std_misc/threads/testcase_mapreduce.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,13 @@ fn main() {
103103
* Collect our intermediate results, and combine them into a final result
104104
************************************************************************/
105105
106-
// collect each thread's intermediate results into a new Vec
107-
let mut intermediate_sums = vec![];
108-
for child in children {
109-
// collect each child thread's return-value
110-
let intermediate_sum = child.join().unwrap();
111-
intermediate_sums.push(intermediate_sum);
112-
}
113-
114-
// combine all intermediate sums into a single final sum.
106+
// combine each thread's intermediate results into a single final sum.
115107
//
116108
// we use the "turbofish" ::<> to provide sum() with a type hint.
117109
//
118110
// TODO: try without the turbofish, by instead explicitly
119111
// specifying the type of final_result
120-
let final_result = intermediate_sums.iter().sum::<u32>();
112+
let final_result = children.into_iter().map(|c| c.join().unwrap()).sum::<u32>();
121113
122114
println!("Final sum result: {}", final_result);
123115
}

0 commit comments

Comments
 (0)