#Insert item into hash set and get reference to it

1 messages · Page 1 of 1 (latest)

bleak urchin
#

I have an owned object, and I want to move it into a hash set, but also get an immutable reference to that item in the hash set.
This seems like a perfect use case for the get_or_insert function, but this is apparently only experimental, and I would like to keep my codebase on the stable branch if possible.
I wish I could just insert the item, and then call .get(ITEM).unwrap() but I can't because the item has been moved into the hashset.
Is there a way of doing this that I'm not thinking of?

gilded robin
#

entry and or_insert

#

my bad, thought you were talking about hashmap

#

Yeah that seems like it should be possible but isn't. Kinda weird.

#

I think the simplest solution is to add hashbrown as a dependency and use that get_or_insert. It's the same as std's but you don't need nightly.

bleak urchin
gilded robin
#

It's the same. It's what the std library uses.

random apex
#

Oh wait

gilded robin
#

Yeah

#

lol

bleak urchin
random apex
#

That's probably an old claim

#

It's literally the same impl

short aurora
#

probably true

#

it does use a different hasher by default tho

random apex
#

Std hashmap is hashbrown hashmap, though

#

As in, std has a dependency on hashbrown

bleak urchin
#

Oh I see so the rust standard library uses hashbrown now, and its 2x faster than what the standard library used to use

#

That makes sense

gilded robin
#

Oh and hashbrown::HashSet has entry!

random apex
#

Hashbrown is basically "hey, I would like all of the cool APIs std doesn't yet have thanks"

bleak urchin
#

Gotcha

gilded robin
#

... but or_insert doesn't return a reference to it.