diff options
| author | Kevin Reid <kpreid@google.com> | 2021-10-13 08:46:34 -0700 |
|---|---|---|
| committer | Kevin Reid <kpreid@switchb.org> | 2024-12-21 19:57:42 -0800 |
| commit | b5e8a5d3939935eecfe8ed2dbc97ad748e090d90 (patch) | |
| tree | 30ea9b2e2175ca4883f850184078dee6fdbb3ab4 | |
| parent | 13170cd787cb733ed24842ee825bcbd98dc01476 (diff) | |
| download | rust-b5e8a5d3939935eecfe8ed2dbc97ad748e090d90.tar.gz rust-b5e8a5d3939935eecfe8ed2dbc97ad748e090d90.zip | |
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.
| -rw-r--r-- | library/alloc/src/collections/btree/map.rs | 9 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/set.rs | 4 | ||||
| -rw-r--r-- | library/std/src/collections/hash/map.rs | 9 | ||||
| -rw-r--r-- | library/std/src/collections/hash/set.rs | 4 |
4 files changed, 25 insertions, 1 deletions
diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index d1ce4e215ed..56679eaa118 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 the last value for each such key are discarded. 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 the last entry for each such key + /// are discarded. /// /// ``` /// use std::collections::BTreeMap; diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 6f8c3b2d152..9f5a42b5f6d 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -1491,6 +1491,10 @@ 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 the last instance of each are discarded. + /// + /// # Examples + /// /// ``` /// use std::collections::BTreeSet; /// diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 109bc394634..5b2cd53763b 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -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 the last entry for each such key + /// are discarded. + /// /// # 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 the last value for each such key are discarded. 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); diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 4c81aaff458..8e840aaa07f 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1091,6 +1091,10 @@ 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 the last instance of each are discarded. + /// /// # Examples /// /// ``` |
