diff options
| author | Jacob Pratt <jacob@jhpratt.dev> | 2024-12-26 21:56:48 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-26 21:56:48 -0500 |
| commit | 9551808f424c6c852e3c836699498afe644fe0bf (patch) | |
| tree | 0e2a2d1d11696443e99fe85af03e50be45ec1e0e /library/alloc/src | |
| parent | b618af13cf14171843ec6f62691c5534f44667af (diff) | |
| parent | 6a43716ada10c18d6991dd9408ce2f31214cb8f3 (diff) | |
| download | rust-9551808f424c6c852e3c836699498afe644fe0bf.tar.gz rust-9551808f424c6c852e3c836699498afe644fe0bf.zip | |
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.
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/collections/btree/map.rs | 9 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/set.rs | 5 |
2 files changed, 13 insertions, 1 deletions
diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index d1ce4e215ed..6d305386dbf 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -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(); @@ -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; diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 6f8c3b2d152..9660023d694 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -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; /// |
