1
Fork 0

Rollup merge of #134644 - kpreid:duplicates, r=Mark-Simulacrum

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 #89869.
This commit is contained in:
Jacob Pratt 2024-12-26 21:56:48 -05:00 committed by GitHub
commit 9551808f42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 1 deletions

View file

@ -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
///
/// ```
@ -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);

View file

@ -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
///
/// ```