Skip to content

Commit 9551808

Browse files
authored
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.
2 parents b618af1 + 6a43716 commit 9551808

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

library/alloc/src/collections/btree/map.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,10 @@ impl<K, V> FusedIterator for RangeMut<'_, K, V> {}
22892289

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

@@ -2403,7 +2407,10 @@ where
24032407

24042408
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
24052409
impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> {
2406-
/// Converts a `[(K, V); N]` into a `BTreeMap<(K, V)>`.
2410+
/// Converts a `[(K, V); N]` into a `BTreeMap<K, V>`.
2411+
///
2412+
/// If any entries in the array have equal keys,
2413+
/// all but one of the corresponding values will be dropped.
24072414
///
24082415
/// ```
24092416
/// use std::collections::BTreeMap;

library/alloc/src/collections/btree/set.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,11 @@ impl<T: Ord, A: Allocator + Clone> BTreeSet<T, A> {
14911491
impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> {
14921492
/// Converts a `[T; N]` into a `BTreeSet<T>`.
14931493
///
1494+
/// If the array contains any equal values,
1495+
/// all but one will be dropped.
1496+
///
1497+
/// # Examples
1498+
///
14941499
/// ```
14951500
/// use std::collections::BTreeSet;
14961501
///

library/std/src/collections/hash/map.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,11 @@ impl<K, V, const N: usize> From<[(K, V); N]> for HashMap<K, V, RandomState>
14461446
where
14471447
K: Eq + Hash,
14481448
{
1449+
/// Converts a `[(K, V); N]` into a `HashMap<K, V>`.
1450+
///
1451+
/// If any entries in the array have equal keys,
1452+
/// all but one of the corresponding values will be dropped.
1453+
///
14491454
/// # Examples
14501455
///
14511456
/// ```
@@ -3219,6 +3224,10 @@ where
32193224
K: Eq + Hash,
32203225
S: BuildHasher + Default,
32213226
{
3227+
/// Constructs a `HashMap<K, V>` from an iterator of key-value pairs.
3228+
///
3229+
/// If the iterator produces any pairs with equal keys,
3230+
/// all but one of the corresponding values will be dropped.
32223231
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> HashMap<K, V, S> {
32233232
let mut map = HashMap::with_hasher(Default::default());
32243233
map.extend(iter);

library/std/src/collections/hash/set.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,11 @@ impl<T, const N: usize> From<[T; N]> for HashSet<T, RandomState>
10911091
where
10921092
T: Eq + Hash,
10931093
{
1094+
/// Converts a `[T; N]` into a `HashSet<T>`.
1095+
///
1096+
/// If the array contains any equal values,
1097+
/// all but one will be dropped.
1098+
///
10941099
/// # Examples
10951100
///
10961101
/// ```

0 commit comments

Comments
 (0)