// Straight from Stack Overflow: https://stackoverflow.com/questions/43921436/extend-iterator-with-a-mean-method use std::time::Duration; pub trait MeanExt: Iterator { fn mean(self) -> M where M: Mean, Self: Sized, { M::mean(self) } } impl MeanExt for I {} pub trait Mean { fn mean(iter: I) -> Self where I: Iterator; } impl Mean for Duration { fn mean(iter: I) -> Self where I: Iterator, { let mut sum = Duration::ZERO; let mut count: u32 = 0; for v in iter { sum += v; count += 1; } if count > 0 { sum / count } else { sum } } } impl<'a> Mean<&'a Duration> for Duration { fn mean(iter: I) -> Self where I: Iterator, { iter.mean() } }