如何使用折叠对向量求和? [英] How do I sum a vector using fold?
问题描述
本 Rust 教程解释了 fold()
机制很好,这个例子代码:
This Rust tutorial explains the fold()
mechanism well, and this example code:
let sum = (1..4).fold(0, |sum, x| sum + x);
按预期工作.
我想在一个向量上运行它,所以基于那个例子,我首先写了这个:
I'd like to run it on a vector, so based on that example, first I wrote this:
let sum: u32 = vec![1,2,3,4,5,6].iter().fold(0, |sum, val| sum += val);
抛出错误:
error: binary assignment operation `+=` cannot be applied to types `_` and `&u32` [E0368]
let sum = ratings.values().fold(0, |sum, val| sum += val);
^~~~~~~~~~
由于某种原因,我猜这可能是与引用相关的错误,因此我将其更改为 fold(0, |sum, &val| sum += val)
,结果是
I guessed this might be a reference-related error for some reason, so I changed that to fold(0, |sum, &val| sum += val)
, which resulted in
error: mismatched types:
expected `u32`,
found `()`
嗯,也许关闭有问题?使用 {sum += x;总和}
,我得到了
Hm, maybe something's wrong with the closure? Using {sum += x; sum }
, I got
binary assignment operation `+=` cannot be applied to types `_` and `&u32`
再来一次.
经过进一步的反复试验,将 mut
添加到 sum
工作:
After further trial and error, adding mut
to sum
worked:
let sum = vec![1,2,3,4,5,6].iter().fold(0, |mut sum, &x| {sum += x; sum});
有人可以解释为什么向量的 fold()
与教程如此不同的原因吗?或者有没有更好的方法来处理这个问题?
Could someone explain the reason why fold()
for vectors differs so much from the tutorial? Or is there a better way to handle this?
作为参考,我使用的是 Rust 测试版,2015 年 4 月 2 日.
For reference, I'm using Rust beta, 2015-04-02.
推荐答案
从 Rust 1.11 开始,你可以 sum
直接迭代器,跳过fold
:
Since Rust 1.11, you can sum
the iterator directly, skipping fold
:
let sum: u32 = vec![1, 2, 3, 4, 5, 6].iter().sum();
<小时>
您已经发现 +=
是问题所在,但我想提供更多说明.
You've already figured out that +=
is the problem, but I'd like to provide some more exposition.
在您的情况下,提供给 fold
闭包的参数是 _
和 &u32
.第一种类型是尚未指定的整数.如果您将 fold 调用更改为 fold(0u32, |sum, val| sum += val)
,您会得到稍微不同的消息:
In your case, the arguments provided to the fold
closure are _
and &u32
. The first type is an not-yet-specified integer. If you change your fold call to fold(0u32, |sum, val| sum += val)
, you'll get a slightly different message:
let sum: u32 = vec![1,2,3,4,5,6].iter().fold(0u32, |sum, val| sum += val);
error[E0308]: mismatched types
|
2 | let sum: u32 = vec![1,2,3,4,5,6].iter().fold(0u32, |sum, val| sum += val);
| ^^^ expected u32, found &{integer}
|
= note: expected type `u32`
= note: found type `&{integer}`
二进制赋值运算+=
的结果值为()
,单位类型.这解释了当您更改为 fold(0, |sum, &val| sum += val)
时的错误消息:
The result value of the binary assignment operation +=
is ()
, the unit type. This explains the error message when you changed to fold(0, |sum, &val| sum += val)
:
let mut a = 1;
let what_am_i = a += 1;
println!("{:?}", what_am_i); // => ()
如果您更改为 fold(0, |sum, &val| {sum += val ; sum})
,则会得到关于不可变变量的可以理解的错误:
If you change to fold(0, |sum, &val| {sum += val ; sum})
, you then get an understandable error about immutable variables:
let sum: u32 = vec![1,2,3,4,5,6].iter().fold(0, |sum, &val| {sum += val; sum});
error[E0384]: re-assignment of immutable variable `sum`
--> src/main.rs:2:66
|
2 | let sum: u32 = vec![1,2,3,4,5,6].iter().fold(0, |sum, &val| {sum += val; sum});
| --- ^^^^^^^^^^ re-assignment of immutable variable
| |
| first assignment to `sum`
从这里开始,您可以将 sum
标记为可变的,但正确的解决方案是简单地使用 sum + val
折叠,正如您所发现的.
From here, you could mark sum
as mutable, but the correct solution is to simply fold with sum + val
, as you discovered.
这篇关于如何使用折叠对向量求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!