是否存在一些适用于所有数字类型(int / uint / float)的代码?
我已经尝试了以下实现,但没有用:
use std::iter::Sum;
#[derive(Default)]
struct VecAccumulator {
memory: Vec<f64>,
}
impl VecAccumulator {
pub fn push(&mut self, n: f64) {
self.memory.push(n);
}
pub fn average(&mut self) -> f64 {
return Sum::sum(self.memory.iter()) / self.memory.len();
}
}
错误是:
error[E0283]: type annotations required: cannot resolve `_: std::iter::Sum<&f64>`
--> src/lib.rs:14:16
|
14 | return Sum::sum(self.memory.iter()) / self.memory.len();
| ^^^^^^^^
|
= note: required by `std::iter::Sum::sum`