mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-04 01:42:54 +03:00
Comment why get_or_insert returns &T
This commit is contained in:
@@ -637,6 +637,8 @@ pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
|
||||
#[inline]
|
||||
#[unstable(feature = "hash_set_entry", issue = "60896")]
|
||||
pub fn get_or_insert(&mut self, value: T) -> &T {
|
||||
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
|
||||
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
|
||||
self.map.raw_entry_mut().from_key(&value).or_insert(value, ()).0
|
||||
}
|
||||
|
||||
@@ -667,6 +669,8 @@ pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
|
||||
Q: Hash + Eq,
|
||||
F: FnOnce(&Q) -> T
|
||||
{
|
||||
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
|
||||
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
|
||||
self.map.raw_entry_mut().from_key(value).or_insert_with(|| (f(value), ())).0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user