<分区>
下面的代码无法推断s的类型
fn main() {
let l: Vec<u32> = vec![];
let s = l.iter().sum();
println!("{:?}", s);
}
这是受 Rust by Example 中的某些东西的启发 https://rustbyexample.com/std_misc/threads/testcase_mapreduce.html
// 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.
//
// we use the "turbofish" ::<> to provide sum() with a type hint.
//
// TODO: try without the turbofish, by instead explicitly
// specifying the type of intermediate_sums
let final_result = intermediate_sums.iter().sum::<u32>();
这似乎暗示这应该是可能的。还是我误解了这个建议?
注意我看到一些相关的票证,例如 Why can't Rust infer the resulting type of Iterator::sum? ,但是在这种情况下,没有为序列指定类型。