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?
No any search results
You already invited:
2 Answers
Brimax
Upvotes from:
you avoid unnecessary allocations, but you will have to do the lookup into the HashMap twice on misses (entry avoids that)
Carter
Upvotes from: