This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
0
Having trouble removing .copied()
Post Body
Hello, how do I remove .copied()
from u.iter().copied().mean_arithmetic()
?
I am trying to make it so I only have to call iter().mean_arithmetic()
or into_iter().mean_arithmetic()
, but the problem I'm having right now is when I convert impl<I> Iterator for MeanArithmetic<I>
to handle &'a T
it wont work for u.into_iter().mean_arithmetic()
.
use num::ToPrimitive;
pub struct MeanArithmetic<I> {
iter: I,
result: f64,
n: usize,
}
impl<I> MeanArithmetic<I>
where
I: Iterator,
{
pub fn new(iter: I) -> MeanArithmetic<I> {
MeanArithmetic {
iter,
result: 0.0,
n: 0,
}
}
}
impl<I> Iterator for MeanArithmetic<I>
where
I: Iterator,
I::Item: ToPrimitive,
{
type Item = f64;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let value = self.iter.next()?.to_f64()?;
self.n = 1;
self.result = (value - self.result) / self.n as f64;
Some(self.result)
}
}
pub trait MeanExt: Iterator {
fn mean_arithmetic(self) -> MeanArithmetic<Self>
where
Self: Sized,
{
MeanArithmetic::new(self)
}
}
impl<I: Iterator> MeanExt for I {}
fn main() {
let u = vec![1, 3];
for n in u.iter().copied().mean_arithmetic() {
println!("{}", n);
}
for n in u.into_iter().mean_arithmetic() {
println!("{}", n);
}
}
Author
Account Strength
90%
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
3,440
Link Karma
52
Comment Karma
3,388
Profile updated: 2 days ago
Posts updated: 3 weeks ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 10 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/rust/commen...