Skip to content
/ rust Public
forked from rust-lang/rust

Commit

Permalink
Rollup merge of rust-lang#134644 - kpreid:duplicates, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Document collection `From` and `FromIterator` impls that drop duplicate keys.

This behavior is worth documenting because there are other plausible alternatives, such as panicking when a duplicate is encountered, and it reminds the programmer to consider whether they should, for example, coalesce duplicate keys first.

Followup to rust-lang#89869.
  • Loading branch information
jhpratt authored Dec 27, 2024
2 parents b618af1 + 6a43716 commit 9551808
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,10 @@ impl<K, V> FusedIterator for RangeMut<'_, K, V> {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
/// Constructs a `BTreeMap<K, V>` from an iterator of key-value pairs.
///
/// If the iterator produces any pairs with equal keys,
/// all but one of the corresponding values will be dropped.
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> BTreeMap<K, V> {
let mut inputs: Vec<_> = iter.into_iter().collect();

Expand Down Expand Up @@ -2403,7 +2407,10 @@ where

#[stable(feature = "std_collections_from_array", since = "1.56.0")]
impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> {
/// Converts a `[(K, V); N]` into a `BTreeMap<(K, V)>`.
/// Converts a `[(K, V); N]` into a `BTreeMap<K, V>`.
///
/// If any entries in the array have equal keys,
/// all but one of the corresponding values will be dropped.
///
/// ```
/// use std::collections::BTreeMap;
Expand Down
5 changes: 5 additions & 0 deletions library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,11 @@ impl<T: Ord, A: Allocator + Clone> BTreeSet<T, A> {
impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> {
/// Converts a `[T; N]` into a `BTreeSet<T>`.
///
/// If the array contains any equal values,
/// all but one will be dropped.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
Expand Down
9 changes: 9 additions & 0 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,11 @@ impl<K, V, const N: usize> From<[(K, V); N]> for HashMap<K, V, RandomState>
where
K: Eq + Hash,
{
/// Converts a `[(K, V); N]` into a `HashMap<K, V>`.
///
/// If any entries in the array have equal keys,
/// all but one of the corresponding values will be dropped.
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -3219,6 +3224,10 @@ where
K: Eq + Hash,
S: BuildHasher + Default,
{
/// Constructs a `HashMap<K, V>` from an iterator of key-value pairs.
///
/// If the iterator produces any pairs with equal keys,
/// all but one of the corresponding values will be dropped.
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> HashMap<K, V, S> {
let mut map = HashMap::with_hasher(Default::default());
map.extend(iter);
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,11 @@ impl<T, const N: usize> From<[T; N]> for HashSet<T, RandomState>
where
T: Eq + Hash,
{
/// Converts a `[T; N]` into a `HashSet<T>`.
///
/// If the array contains any equal values,
/// all but one will be dropped.
///
/// # Examples
///
/// ```
Expand Down

0 comments on commit 9551808

Please sign in to comment.