How can I write a code that updates a value if a key exists already, or creates a new one if needed?

Hi, I have a HashMap<String, Something>, and I have a potential key as a &str (not owned by me). How can I write a code that updates a value if a key exists already, or creates a new one if needed? I'm guessing entry doesn't help me here since it expects a String, and I don't want it to be constructed for every single lookup... any ideas?
You already invited:

Brimax

Upvotes from:

something like this: https://play.rust-lang.org/%3F ... 0e94c
you avoid unnecessary allocations, but you will have to do the lookup into the HashMap twice on misses (entry avoids that)

Carter

Upvotes from:

Not sure but you are probabyl fixing what isn't too broken. In release unnecessary allocation could be easily optimized away

If you wanna answer this question please Login or Register