Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b5e8a5d

Browse files
committed
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.
1 parent 13170cd commit b5e8a5d

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
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 the last value for each such key are discarded.
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, all but the last entry for each such key
2413+
/// are discarded.
24072414
///
24082415
/// ```
24092416
/// use std::collections::BTreeMap;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,10 @@ 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, all but the last instance of each are discarded.
1495+
///
1496+
/// # Examples
1497+
///
14941498
/// ```
14951499
/// use std::collections::BTreeSet;
14961500
///

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

Lines changed: 9 additions & 0 deletions
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, all but the last entry for each such key
1452+
/// are discarded.
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 the last value for each such key are discarded.
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,10 @@ 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, all but the last instance of each are discarded.
1097+
///
10941098
/// # Examples
10951099
///
10961100
/// ```

0 commit comments

Comments
 (0)