HashMap insertion

⚓ Rust    📅 2026-06-02    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: HashMap insertion

Is there an actual way to insert an element into a HashMap/HashSet so that:

  1. It does not replace the current value if the key was already present
  2. It returns if the key was already present in some way or another (bool/Option/Result/etc.)
  3. It does not do lookup twice
  4. The provided key is not necessarily of type K but is of type Q such that K : Borrow<Q> + From<Q> and it only constructs the key (K) from the given lookup key (Q) if it turns out that the key was not yet present and thus now needs to be constructed for insertion
  5. It can be done in stable Rust

I don't care if it's a single function or a multitude of statements.

4 posts - 4 participants

Read full topic

🏷️ Rust_feed