我如何为某些通用类型实现通用特征的默认实现?
How can I implement a default implementation of a generic trait for some generic type?
我想在一些通用类型 Vec<T>
上实现特征 MovingAverage
的默认实现,但我显然做错了什么,因为我可以让它适用于具体类型 Vec<f64>
但不是具体类型 Vec<i32>
。这是错误的输出:
error[E0599]: the method `sma` exists for struct `Vec<{integer}>`, but its trait bounds were not satisfied
--> src/main.rs:8:22
|
8 | let smai = numsi.sma(n);
| ^^^ method cannot be called on `Vec<{integer}>` due to unsatisfied trait bounds
|
::: /home/czar/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:397:1
|
397 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<{integer}>: MovingAverage`
|
= note: the following trait bounds were not satisfied:
`f64: AddAssign<{integer}>`
which is required by `Vec<{integer}>: MovingAverage`
`f64: SubAssign<{integer}>`
which is required by `Vec<{integer}>: MovingAverage`
这是我来自 ma.rs 的代码:
pub trait MovingAverage<Output = Vec<f64>> {
fn sma(&self, periods: usize) -> Output;
}
impl<T> MovingAverage for Vec<T>
where
T: Copy + Num,
f64: AddAssign<T> + SubAssign<T>,
{
fn sma(&self, periods: usize) -> Vec<f64> {
let mut sum = 0f64;
let mut ma = Vec::<f64>::new();
for i in 0..self.len() {
if i >= periods {
ma.push(sum / periods as f64);
sum -= self[i - periods];
}
sum += self[i];
}
ma
}
}
我的main.rs
let numsf = vec![5., 10., 3., 9., 8., 7.];
let mut numsi = vec![2, 4, 3, 5, 1, 1];
let n = 2;
let smaf = numsf.sma(n);
let smai = numsi.sma(n); // doesnt work here
我的错误是怎么回事,在泛型上实现特性而不需要为每个具体类型实现特性的正确方法是什么?提前致谢,如果您需要进一步说明,请告诉我。
也许更好的界限是 T: Into<f64>
。然后,您可以在访问它时将每个元素简单地转换为 f64
。此外,如果您为 [T]
而不是 Vec<T>
实现特征,它也适用于切片。试试这个:
impl<T: Copy + Into<f64>> MovingAverage for [T] {
fn sma(&self, periods: usize) -> Vec<f64> {
let mut sum = 0f64;
let mut ma = Vec::<f64>::new();
for i in 0..self.len() {
if i >= periods {
ma.push(sum / periods as f64);
sum -= self[i - periods].into();
}
sum += self[i].into();
}
ma
}
}
(旁注:我认为您的代码可能无意中忽略了输入的最后一个元素)
我想在一些通用类型 Vec<T>
上实现特征 MovingAverage
的默认实现,但我显然做错了什么,因为我可以让它适用于具体类型 Vec<f64>
但不是具体类型 Vec<i32>
。这是错误的输出:
error[E0599]: the method `sma` exists for struct `Vec<{integer}>`, but its trait bounds were not satisfied
--> src/main.rs:8:22
|
8 | let smai = numsi.sma(n);
| ^^^ method cannot be called on `Vec<{integer}>` due to unsatisfied trait bounds
|
::: /home/czar/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:397:1
|
397 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<{integer}>: MovingAverage`
|
= note: the following trait bounds were not satisfied:
`f64: AddAssign<{integer}>`
which is required by `Vec<{integer}>: MovingAverage`
`f64: SubAssign<{integer}>`
which is required by `Vec<{integer}>: MovingAverage`
这是我来自 ma.rs 的代码:
pub trait MovingAverage<Output = Vec<f64>> {
fn sma(&self, periods: usize) -> Output;
}
impl<T> MovingAverage for Vec<T>
where
T: Copy + Num,
f64: AddAssign<T> + SubAssign<T>,
{
fn sma(&self, periods: usize) -> Vec<f64> {
let mut sum = 0f64;
let mut ma = Vec::<f64>::new();
for i in 0..self.len() {
if i >= periods {
ma.push(sum / periods as f64);
sum -= self[i - periods];
}
sum += self[i];
}
ma
}
}
我的main.rs
let numsf = vec![5., 10., 3., 9., 8., 7.];
let mut numsi = vec![2, 4, 3, 5, 1, 1];
let n = 2;
let smaf = numsf.sma(n);
let smai = numsi.sma(n); // doesnt work here
我的错误是怎么回事,在泛型上实现特性而不需要为每个具体类型实现特性的正确方法是什么?提前致谢,如果您需要进一步说明,请告诉我。
也许更好的界限是 T: Into<f64>
。然后,您可以在访问它时将每个元素简单地转换为 f64
。此外,如果您为 [T]
而不是 Vec<T>
实现特征,它也适用于切片。试试这个:
impl<T: Copy + Into<f64>> MovingAverage for [T] {
fn sma(&self, periods: usize) -> Vec<f64> {
let mut sum = 0f64;
let mut ma = Vec::<f64>::new();
for i in 0..self.len() {
if i >= periods {
ma.push(sum / periods as f64);
sum -= self[i - periods].into();
}
sum += self[i].into();
}
ma
}
}
(旁注:我认为您的代码可能无意中忽略了输入的最后一个元素)