rust-lang / rust-lang/rfcs

Entry::insert

Open
#2,104 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I'm currently in a position of wanting an Entry::insert (or other bikesheddable name) for when you want to conditionally update a value - inserting a value if there is none and updating it if it exists but is "worse" by some metric. This would be nicely captured with code similar to the following:

let was_updated = match map.entry(key) {
    Entry::Occupied(ref entry) if !new_val.better_than(entry.get()) => false,
    otherwise => { otherwise.upsert(new_val); true },
};

Instead, you have to do:

let was_updated = match map.entry(key) {
    Entry::Occupied(ref entry) if !new_val.better_than(entry.get()) => false,
    Entry::Occupied(entry) => { entry.insert(new_val); true },
    Entry::Vacant(entry) => { entry.insert(new_val); true },
};

Which is redundant. The two insert methods have different signatures but I would imagine that Entry::insert would return an Result<&mut Value, Value> to pass ownership in the case that the entry is occupied (or maybe a CowMut if that was to exist).

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

Read the Entry::insert proposal and its examples in this issue, then compare the existing Entry::Occupied and Entry::Vacant paths described there. Clarify the intended ownership, return behavior, and method name through the RFC process; done means the API design is settled and recorded.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api
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.