rust-lang / rust-lang/hashbrown

Feature request: Add `get_or_compute`/`compute_if_absent` function

Open
#401 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3k
Forks
358
Avg merge
11h 57m
Merged PRs (30d)
2

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing get_or_insert_with behavior and the discussions in #399 and #396; the issue does not name files or tests. Clarify the API and map/set semantics, then add coverage showing lookup, computation, insertion, and returned references behave as specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.