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

Iterator over entries of Hash* #33549

Closed
nagisa opened this issue May 10, 2016 · 4 comments
Closed

Iterator over entries of Hash* #33549

nagisa opened this issue May 10, 2016 · 4 comments
Labels
A-collections Area: `std::collection` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented May 10, 2016

An iterator over entries of HashMap/Set could be added?

@nagisa nagisa added the A-collections Area: `std::collection` label May 10, 2016
@eefriedman
Copy link
Contributor

HashMap already has a method iter_mut(); could you describe what you want a bit more precisely?

@nagisa
Copy link
Member Author

nagisa commented May 10, 2016

@eefriedman Consider following code which could potentially be a poor-man’s implementation of pop:

for entry in map.entries() {
    if let Entry::Occupied(o) = entry {
        return o.remove();
    } else { unreachable!() }
}

That cannot be implemented with iter_mut() today.

@apasel422
Copy link
Contributor

I'm not sure that the current HashMap design would support the existence of multiple OccupiedEntrys at the same time (due to the relocation of entries during removal), but if such an iterator were added, I expect that its item type would be OccupiedEntry rather than Entry, as a VacantEntry cannot be created without a key.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 25, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 15, 2017

I agree with @apasel422 that I don't think this will work with an iterator of entries. The Iterator trait permits the caller to hold on to multiple items at the same time, which in this case would mean multiple items that each have mutable access to the same HashMap.

I think the use case of iterating over key-value pairs and conditionally removing them efficiently is addressed by HashMap::retain which was added in #36648, after this issue was opened, so I think this can be considered done.

@dtolnay dtolnay closed this as completed Nov 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants