You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, to insert a new value into a std::collections::HashMap, one must transfer ownership of the key to the map. The map however does not seem to require ownership of the key.
The HashMap does not keep around a copy of the key however. It only stores its hash. This means that there is no way to get the key out of the HashMap again.
For example, take the signature of the insert method:
fninsert(&mutself,k:K,v:V) -> Option<V>
If the value is updated, it returns the old value, thus giving the user ownership of said value again. The key however, is lost forever. There is no way to get it back.
The text was updated successfully, but these errors were encountered:
Binero
changed the title
HashMap should not take its keys by valueHashMap should not take ownership of its keys
Sep 2, 2017
Aha, my bad. I got confused just now because surely it must store the key SOMEWHERE, otherwise the HashMap wouldn't be able to check within a bucket. The entry is exactly what I was looking for, thanks a ton!
Right now, to insert a new value into a
std::collections::HashMap
, one must transfer ownership of the key to the map. The map however does not seem to require ownership of the key.The
HashMap
does not keep around a copy of the key however. It only stores its hash. This means that there is no way to get the key out of theHashMap
again.For example, take the signature of the insert method:
If the value is updated, it returns the old value, thus giving the user ownership of said value again. The key however, is lost forever. There is no way to get it back.
The text was updated successfully, but these errors were encountered: