【发布时间】:2018-08-21 08:58:47
【问题描述】:
以下代码无法推断出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?,但在这种情况下,没有为序列指定类型。
【问题讨论】:
-
这与您链接的问题相同。该问题中也给出了序列的类型(它是
Range<i32>)。 -
我与 Rust by Example 团队确认这是一个拼写错误。
标签: rust