我有一个最小堆 i32
在 Rust 中,我想计算其元素的总和并将其存储在 i32
中.
let mut total_sum = 0;
for current in min_heap {
total_sum = current + total_sum;
}
编译时出现以下错误:
cannot add `{integer}` to `Reverse<i32>`
最佳答案
您只需调用sum
即可映射后迭代器上的方法 Reverse<i32>
位于你的堆中,以获取其内部值。
let total_sum: i32 = min_heap.into_iter()
.map(|i| i.0)
.sum();
一些建议:
- 避免突变;
- 请勿使用
x = x + y
,使用x += y
相反; - 不要在函数和变量名称中使用驼峰命名法;
- 不要使用换行大括号。
关于rust - 如何从 std::cmp::Reverse::<T> 获取 T 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74734489/