Skip to content

Commit 9161a4d

Browse files
committed
Comment why get_or_insert returns &T
1 parent 5e2c9d3 commit 9161a4d

File tree

1 file changed

+4
-0
lines changed
  • src/libstd/collections/hash

1 file changed

+4
-0
lines changed

src/libstd/collections/hash/set.rs

+4
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ impl<T, S> HashSet<T, S>
637637
#[inline]
638638
#[unstable(feature = "hash_set_entry", issue = "60896")]
639639
pub fn get_or_insert(&mut self, value: T) -> &T {
640+
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
641+
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
640642
self.map.raw_entry_mut().from_key(&value).or_insert(value, ()).0
641643
}
642644

@@ -667,6 +669,8 @@ impl<T, S> HashSet<T, S>
667669
Q: Hash + Eq,
668670
F: FnOnce(&Q) -> T
669671
{
672+
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
673+
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
670674
self.map.raw_entry_mut().from_key(value).or_insert_with(|| (f(value), ())).0
671675
}
672676

0 commit comments

Comments
 (0)