Skip to content

Commit 921579c

Browse files
committed
Add or_insert_with_key to Entry of HashMap/BTreeMap
Going along with or_insert_with, or_insert_with_key provides the Entry's key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time. This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.
1 parent 93dc97a commit 921579c

File tree

2 files changed

+54
-0
lines changed
  • src
    • liballoc/collections/btree
    • libstd/collections/hash

2 files changed

+54
-0
lines changed

src/liballoc/collections/btree/map.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,33 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
23782378
}
23792379
}
23802380

2381+
#[unstable(feature = "or_insert_with_key", issue = "70996")]
2382+
/// Ensures a value is in the entry by inserting, if empty, the result of the default function,
2383+
/// which takes the key as its argument, and returns a mutable reference to the value in the
2384+
/// entry.
2385+
///
2386+
/// # Examples
2387+
///
2388+
/// ```
2389+
/// use std::collections::BTreeMap;
2390+
///
2391+
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2392+
///
2393+
/// map.entry("poneyland").or_insert_with_key(|key| key.chars().count());
2394+
///
2395+
/// assert_eq!(map["poneyland"], 9);
2396+
/// ```
2397+
#[inline]
2398+
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
2399+
match self {
2400+
Occupied(entry) => entry.into_mut(),
2401+
Vacant(entry) => {
2402+
let value = default(entry.key());
2403+
entry.insert(value)
2404+
}
2405+
}
2406+
}
2407+
23812408
/// Returns a reference to this entry's key.
23822409
///
23832410
/// # Examples

src/libstd/collections/hash/map.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,33 @@ impl<'a, K, V> Entry<'a, K, V> {
19431943
}
19441944
}
19451945

1946+
#[unstable(feature = "or_insert_with_key", issue = "70996")]
1947+
/// Ensures a value is in the entry by inserting, if empty, the result of the default function,
1948+
/// which takes the key as its argument, and returns a mutable reference to the value in the
1949+
/// entry.
1950+
///
1951+
/// # Examples
1952+
///
1953+
/// ```
1954+
/// use std::collections::HashMap;
1955+
///
1956+
/// let mut map: HashMap<&str, usize> = HashMap::new();
1957+
///
1958+
/// map.entry("poneyland").or_insert_with_key(|key| key.chars().count());
1959+
///
1960+
/// assert_eq!(map["poneyland"], 9);
1961+
/// ```
1962+
#[inline]
1963+
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
1964+
match self {
1965+
Occupied(entry) => entry.into_mut(),
1966+
Vacant(entry) => {
1967+
let value = default(entry.key());
1968+
entry.insert(value)
1969+
}
1970+
}
1971+
}
1972+
19461973
/// Returns a reference to this entry's key.
19471974
///
19481975
/// # Examples

0 commit comments

Comments
 (0)