diff --git a/collections/btree/map.rs b/collections/btree/map.rs index 79042ab..2affbc0 100644 --- a/collections/btree/map.rs +++ b/collections/btree/map.rs @@ -109,7 +109,20 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT; /// } /// ``` /// -/// `BTreeMap` also implements an [`Entry API`], which allows for more complex +/// A `BTreeMap` with a known list of items can be initialized from an array: +/// +/// ``` +/// use std::collections::BTreeMap; +/// +/// let solar_distance = BTreeMap::from([ +/// ("Mercury", 0.4), +/// ("Venus", 0.7), +/// ("Earth", 1.0), +/// ("Mars", 1.5), +/// ]); +/// ``` +/// +/// `BTreeMap` implements an [`Entry API`], which allows for complex /// methods of getting, setting, updating and removing keys and their values: /// /// [`Entry API`]: BTreeMap::entry @@ -2012,6 +2025,20 @@ where } } +#[stable(feature = "std_collections_from_array", since = "1.56.0")] +impl From<[(K, V); N]> for BTreeMap { + /// ``` + /// use std::collections::BTreeMap; + /// + /// let map1 = BTreeMap::from([(1, 2), (3, 4)]); + /// let map2: BTreeMap<_, _> = [(1, 2), (3, 4)].into(); + /// assert_eq!(map1, map2); + /// ``` + fn from(arr: [(K, V); N]) -> Self { + core::array::IntoIter::new(arr).collect() + } +} + impl BTreeMap { /// Gets an iterator over the entries of the map, sorted by key. /// diff --git a/collections/btree/map/tests.rs b/collections/btree/map/tests.rs index 3a74b6a..1e61692 100644 --- a/collections/btree/map/tests.rs +++ b/collections/btree/map/tests.rs @@ -2173,3 +2173,10 @@ fn test_insert_remove_intertwined_ord_chaos() { } map.check_invariants(); } + +#[test] +fn from_array() { + let map = BTreeMap::from([(1, 2), (3, 4)]); + let unordered_duplicates = BTreeMap::from([(3, 4), (1, 2), (1, 2)]); + assert_eq!(map, unordered_duplicates); +} diff --git a/collections/btree/set.rs b/collections/btree/set.rs index 9711214..0c268ad 100644 --- a/collections/btree/set.rs +++ b/collections/btree/set.rs @@ -59,6 +59,14 @@ use super::Recover; /// println!("{}", book); /// } /// ``` +/// +/// A `BTreeSet` with a known list of items can be initialized from an array: +/// +/// ``` +/// use std::collections::BTreeSet; +/// +/// let set = BTreeSet::from([1, 2, 3]); +/// ``` #[derive(Hash, PartialEq, Eq, Ord, PartialOrd)] #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "BTreeSet")] @@ -1057,6 +1065,20 @@ impl FromIterator for BTreeSet { } } +#[stable(feature = "std_collections_from_array", since = "1.56.0")] +impl From<[T; N]> for BTreeSet { + /// ``` + /// use std::collections::BTreeSet; + /// + /// let set1 = BTreeSet::from([1, 2, 3, 4]); + /// let set2: BTreeSet<_> = [1, 2, 3, 4].into(); + /// assert_eq!(set1, set2); + /// ``` + fn from(arr: [T; N]) -> Self { + core::array::IntoIter::new(arr).collect() + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl IntoIterator for BTreeSet { type Item = T; diff --git a/collections/btree/set/tests.rs b/collections/btree/set/tests.rs index 4cb6e3d..de7a10d 100644 --- a/collections/btree/set/tests.rs +++ b/collections/btree/set/tests.rs @@ -738,3 +738,10 @@ fn test_split_off_large_random_sorted() { assert!(set.into_iter().eq(data.clone().into_iter().filter(|x| *x < key))); assert!(right.into_iter().eq(data.into_iter().filter(|x| *x >= key))); } + +#[test] +fn from_array() { + let set = BTreeSet::from([1, 2, 3, 4]); + let unordered_duplicates = BTreeSet::from([4, 1, 4, 3, 2]); + assert_eq!(set, unordered_duplicates); +}