Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,9 @@ impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Ord, V, A: Allocator + Clone> Extend<(K, V)> for BTreeMap<K, V, A> {
/// Add all elements from `iter` to this map by calling [`BTreeMap::insert`]

@camsteffen camsteffen Jun 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid promising that insert is called since that is an implementation detail that could change. It could say:

Inserts each entry into the map. Existing entries with the same key are replaced.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't change in a way that makes it not semantically equivalent to calling insert. I could say Add all elements from iter to this map, as if insert was called for each element or something... but that seems a bit awkward and pedantic.

I think duplicating the insert documentation is not as good because you lose information. For example with your version you don't get this:

The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

/// in a loop. Its return value is ignored. This means duplicate elements
/// will be overwritten.
#[inline]
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
iter.into_iter().for_each(move |(k, v)| {
Expand All @@ -2573,6 +2576,9 @@ impl<K: Ord, V, A: Allocator + Clone> Extend<(K, V)> for BTreeMap<K, V, A> {
impl<'a, K: Ord + Copy, V: Copy, A: Allocator + Clone> Extend<(&'a K, &'a V)>
for BTreeMap<K, V, A>
{
/// Add all elements from `iter` to this map by calling [`BTreeMap::insert`]
Comment thread
Timmmm marked this conversation as resolved.
/// in a loop. Its return value is ignored. This means duplicate elements
/// will be overwritten. Keys and values are copied.
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
}
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,9 @@ where
S: BuildHasher,
A: Allocator,
{
/// Add all elements from `iter` to this map by calling [`HashMap::insert`]
/// in a loop. Its return value is ignored. This means duplicate elements
/// will be overwritten.
#[inline]
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
self.base.extend(iter)
Expand All @@ -3051,6 +3054,9 @@ where
S: BuildHasher,
A: Allocator,
{
/// Add all elements from `iter` to this map by calling [`HashMap::insert`]
/// in a loop. Its return value is ignored. This means duplicate elements
/// will be overwritten. Keys and values are copied.
#[inline]
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
self.base.extend(iter)
Expand Down
Loading