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

iter: decide on the fate of to_owned_vec, sum and product #9496

Closed
thestinger opened this issue Sep 25, 2013 · 9 comments
Closed

iter: decide on the fate of to_owned_vec, sum and product #9496

thestinger opened this issue Sep 25, 2013 · 9 comments

Comments

@thestinger
Copy link
Contributor

The sum and product methods are wrappers around fold, but only work with iterators where the element type A directly implements Add<A, A>/Mul<A, A>. With an iterator of references to numbers, you can do iter().fold(0, |a, b| a + *b), but with sum/product the elements have to be cloned via map (inefficient for big integers, and uglier than using fold).

The to_owned_vec method simply calls collect, and removes the need to specify a type annotation. It's the only method on iterators using a specific container type, which is ugly. A language solution to this problem (partial type hints? collect::<~[_]>()) would be nice.

These are good candidates for being marked with a stability warning attribute.

@huonw
Copy link
Member

huonw commented Sep 25, 2013

(Unfortunately marking them with a stability is blocked on #8961.)

@bluss
Copy link
Member

bluss commented Sep 25, 2013

I'm not sure if it's a solution if .to_owned_vec() is made redundant by collect::<~[_]>() Each of those punctuation symbols cost twice-or-more as much to process or type out compared to the characters in .to_owned_vec().

I think the specialized method is the pragmatic solution, but would it be wrong to look towards something completely different? Say standardizing on ::from to create containers from iterators (Iterables?): HashMap::from(v.move_iter()), std::vec::from(range(..).map(..))

@Kimundi
Copy link
Member

Kimundi commented Sep 25, 2013

@blake2-ppc That's basically FromIterator, with which collect is implemented.

Also, I personally find i much harder to parse nested function calls like that than to chain a method, so I'd be against removing that in general.

to_owned_vec is convenient for the reasons lined out by you, but it probably should not life in Iterator. Make it an extension trait for Iterators and stuff it into std::vec.

@thestinger
Copy link
Contributor Author

@blake2-ppc: It's an extra redundant method for every container to implement and only libstd has the hack of including traits in prelude available.

I'm strongly against including these methods, and also strongly against duplicating trait static methods in the type namespace. Working around language flaws by adding duplicate functions/methods all over the place is awful. I almost regret adding the collect wrapper itself because it set a very bad precedent.

@bluss
Copy link
Member

bluss commented Sep 26, 2013

What would the alternative to .collect() be? Could we like my suggestion, have types implement constructor functions with a ::from() convention: HashMap::from(iter), RingBuf::from(iter)

A thought: A trait doesn't have to be a user-facing interface and vice versa. ::new() is a convention but it's an important one, ::from() could be the same.

@thestinger
Copy link
Contributor Author

collect is implemented on top of the FromIterator trait. It's sugar to work around the pain of using generic static methods, and the lack of type namespaces for built-in types.

Generic conversion from iterators is important, and I am definitely against the usual pattern of duplicating code everywhere.

@bluss
Copy link
Member

bluss commented Sep 26, 2013

oh yeah. In my mind FromIterator was just the mechanism to implement .collect(). I assumed you wanted an alternative to both, I don't propose duplicating code. Of course FromIterator::from_iterator is a bit inconvenient to type.

@thestinger
Copy link
Contributor Author

Yeah, I expect collect to stay around as a necessary evil.

@thestinger
Copy link
Contributor Author

I'm okay with keeping sum and product around. We can implement Add and Mul for references to deal with the annoyance. The to_owned_vec method will be removed.

flip1995 pushed a commit to flip1995/rust that referenced this issue Oct 6, 2022
[`never_loop`]: Fix FP with let..else statements.

Fixes rust-lang#9356

This has been bugging me for a while, so I thought I'd take a stab at it! I'm completely uncertain about the quality of my code, but I think it's an alright start, so opening this PR to get some feedback from more experienced clippy people :)

changelog: [`never_loop`]: Fix FP with let..else statements
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

4 participants