How do I sum a vector using fold?

前端 未结 2 881
清酒与你
清酒与你 2020-12-09 01:28

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

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

work

2条回答
  •  执笔经年
    2020-12-09 01:36

    So it turned out there was a huge difference in my code, as I wrote

    sum += x

    instead of

    sum + x.

    Well, at least I hope this question helps, in case someone gets into similar situation.

提交回复
热议问题