Generic function over integer slice

Whoops, I did not know that PrimInt brings to_f32. With that the custom Trait is not needed in my solution and it becomes

fn average<'a, T>(numbers: &'a[T]) -> f32
where
    T: num::PrimInt + Sum<&'a T>,
{
    let sum : T = numbers.iter().sum();
    sum.to_f32().unwrap() / numbers.len() as f32
}
1 Like