Skip to content
New issue

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

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

Already on GitHub? # to your account

Feature request: Add get_or_compute/compute_if_absent function #401

Open
yyny opened this issue Feb 16, 2023 · 1 comment
Open

Feature request: Add get_or_compute/compute_if_absent function #401

yyny opened this issue Feb 16, 2023 · 1 comment

Comments

@yyny
Copy link

yyny commented Feb 16, 2023

Inspired by computeIfAbsent from Java;

  • Search for a value by key.
  • If no value is found, compute a value and insert it.
  • Return a reference to the value.

For Sets, the expected behavior when they key is not found is to insert into a separate entry, rather than populating the entry associated with the key.

This would provide an easy and efficient path for people looking for this behavior, and avoid misuse of get_or_insert_with, which appears to do the same thing, but doesn't, see #399 and the discussion in #396.

I propose the name get_or_compute rather than following Java, since it makes it harder to accidentally use get_or_insert_with because autocomplete will suggest both.

@Amanieu
Copy link
Member

Amanieu commented Feb 22, 2023

I would prefer fixing the existing method rather than adding another which does almost the same thing. This should be discussed in rust-lang/rust#60896.

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

No branches or pull requests

2 participants