下面的代码无法推断s的类型
fn main() {
let l: Vec<u32> = vec![];
let s = l.iter().sum();
println!("{:?}", s);
}这是由“铁锈”中的某些东西激发的,例如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?,但是在这种情况下,没有给出序列的类型。
发布于 2018-03-13 07:43:33
这似乎意味着这是可能的。还是我误解了这个建议?
我觉得这是误解。
// TODO: try without the turbofish, by instead explicitly
// specifying the type of intermediate_sums
let final_result = intermediate_sums.iter().sum::<u32>();它说,通过显式指定类型,您可以不使用turbo,即执行以下操作:
let final_result: u32 = intermediate_sums.iter().sum();在这方面,您的主要功能可以写成:
fn main() {
let l: Vec<u32> = vec![];
let s: u32 = l.iter().sum();
println!("{:?}", s);
}https://stackoverflow.com/questions/49250494
复制相似问题