Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a reason parameters are Iterators instead of IntoIterator? #14

Open
FelixBenning opened this issue May 7, 2020 · 1 comment

Comments

@FelixBenning
Copy link

FelixBenning commented May 7, 2020

If I understand what I am doing, then, if

pub fn mean<I>(it: I) -> f64 where
    I: Iterator,
    <I as Iterator>::Item: ToPrimitive, 

was changed to something like

fn mean<'a, I, T>(x:I)-> f64 where
    I: IntoIterator<Item= T>,
    T: Into<&'a f64>
{
   let it = x.into_iter();
   ...
}

You could also use mean on vectors. e.g.

mean(&vec![1,2,3])

as opposed to

mean(vec![1,2,3].iter().collect())

without losing any functionality for iterators themselves. But I am just learning Rust, so I might not understand some limitation of this approach.

@BurntSushi
Copy link
Owner

Yes, the API should use IntoIterator. This crate was actually written IntoIterator existed (IIRC), and I just never updated the APIs.

But you don't need vec![1, 2, 3].iter().collect(). Just &[1, 2, 3].iter() should work. Or at least, &[1, 2, 3].iter().cloned().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants