gpt4 book ai didi

rust - 为什么 Rust 不能在给定序列的显式类型时推断 sum 的结果

转载 作者:行者123 更新时间:2023-11-29 08:29:38 25 4
gpt4 key购买 nike

<分区>

下面的代码无法推断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? ,但是在这种情况下,没有为序列指定类型。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com