Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

rust - How do I sum a vector using fold?

This Rust tutorial explains the fold() mechanism well, and this example code:

let sum = (1..4).fold(0, |sum, x| sum + x);

works as expected.

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);

which threw an error:

error: binary assignment operation `+=` cannot be applied to types `_` and `&u32` [E0368]
let sum = ratings.values().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 `()`

Hm, maybe something's wrong with the closure? Using {sum += x; sum }, I got

binary assignment operation `+=` cannot be applied to types `_` and `&u32`

again.

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});

Could someone explain the reason why fold() for vectors differs so much from the tutorial? Or is there a better way to handle this?

For reference, I'm using Rust beta, 2015-04-02.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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}`

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); // => ()

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`

From here, you could mark sum as mutable, but the correct solution is to simply fold with sum + val, as you discovered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...