Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

rust - Implementing mean function for generic types

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The primitive types implement the FromPrimitive trait, defined in the num crate, to allow conversions between primitive types, including usize. We can add a FromPrimitive bound on the function, and then we can convert the usize to a T:

extern crate num; // 0.2.0

use num::{FromPrimitive, Zero};
use std::ops::{Add, Div};

impl<T> Vector<T>
where
    T: Copy + Zero + Add<T, Output = T> + Div<T, Output = T> + FromPrimitive,
{
    pub fn mean(&self) -> T {
        let sum = self.sum();
        sum / FromPrimitive::from_usize(self.data.len()).unwrap()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...