Skip to content

Add into_keys and into_values to associative maps #55214

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

Closed
orlp opened this issue Oct 20, 2018 · 4 comments · Fixed by #75163
Closed

Add into_keys and into_values to associative maps #55214

orlp opened this issue Oct 20, 2018 · 4 comments · Fixed by #75163
Labels
A-collections Area: `std::collections` A-iterators Area: Iterators C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@orlp
Copy link
Contributor

orlp commented Oct 20, 2018

Associative maps (HashMap and BTreeMap) in Rust are currently missing two methods to get owning iterators to their keys or values.

We have iter to get key-value reference pairs, iter_mut to get key-value mutable reference pairs and into_iter to get owned key-value pairs.

I'd expect keys and values to offer a similar trio of methods (with an exception for keys_mut of course). values_mut is there, but the into variants are missing for seemingly no good reason.

As an example implementation (but perhaps we can do better):

pub fn into_keys(self) -> impl Iterator<Item = K> {
    self.into_iter().map(|(k, v)| k)
}

pub fn into_values(self) -> impl Iterator<Item = V> {
    self.into_iter().map(|(k, v)| v)
}
@shepmaster shepmaster added A-collections Area: `std::collections` T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. A-iterators Area: Iterators labels Oct 20, 2018
@dtolnay dtolnay added C-feature-accepted Category: A feature request that has been accepted pending implementation. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Jun 17, 2020
@dtolnay
Copy link
Member

dtolnay commented Jun 17, 2020

I would accept a PR to introduce unstable into_keys() and into_values().

@canova
Copy link
Contributor

canova commented Jul 24, 2020

@dtolnay Hi, I’m interested in working on this but I have a question regarding the implementation. I guess we can implement this in two ways. First one is implementing like the example in the first comment, like returning Map<IntoIter<K, V>>. And the second option is to return a struct of its own (like a mixture of IntoIter and Keys/Values structs) instead of returning Map<IntoIter<T>>. We can name them IntoKeys and IntoValues and they can be responsible of implementing the Iterator trait.
I feel like performance wise they are same, since map and custom struct both will be evaluated lazily when we iterate over. But there might be a rule to always return a proper type in the rust std code that I don't know. So I think I’m okay implementing either way but wanted to ask just in case. What do you think?

@dtolnay
Copy link
Member

dtolnay commented Aug 3, 2020

For the public API I would prefer this implemented using dedicated IntoKeys and IntoValues iterator types. They can be implemented internally as wrappers around Map<IntoIter<K, V>> if that simplifies things.

@canova
Copy link
Contributor

canova commented Aug 4, 2020

Thanks for the answer @dtolnay! Created a PR for this and added you as a reviewer. Would you mind taking a look at it when you have time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collections` A-iterators Area: Iterators C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants