Skip to content

Add into_keys and into_values to associative maps #55214

Closed
@orlp

Description

@orlp

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`A-iteratorsArea: IteratorsC-feature-acceptedCategory: A feature request that has been accepted pending implementation.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions