I'm trying to write a function to return the mean of a Vector
. I want it to work with generic types but I'm having some difficulty implementing it.
extern crate num; // 0.2.0
use num::Zero;
use std::ops::{Add, Div};
pub struct Vector<T> {
pub size: usize,
pub data: Vec<T>,
}
impl<T: Copy + Zero + Add<T, Output = T>> Vector<T> {
pub fn sum(&self) -> T {
self.data.iter().fold(T::zero(), |sum, &val| sum + val)
}
}
impl<T: Copy + Zero + Add<T, Output = T> + Div<T, Output = T>> Vector<T> {
pub fn mean(&self) -> T {
let sum = self.sum();
sum / self.data.len()
}
}
Playground.
The above example doesn't compile as self.data.len()
is a usize
and sum
is of type T
:
error[E0308]: mismatched types
--> src/lib.rs:20:15
|
20 | sum / self.data.len()
| ^^^^^^^^^^^^^^^ expected type parameter, found usize
|
= note: expected type `T`
found type `usize`
I know I could change the signature to:
impl<T: Copy + Zero + Add<T, Output = T> + Div<usize, Output = T>> Vector<T>
It would compile - but this isn't implemented for the Rust primitive types. How should I go about this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…