Skip to content

Commit 817e1b8

Browse files
committed
Auto merge of #44344 - jonhoo:entry_or_default, r=BurntSushi
Add or_default to Entry APIs As argued for in #44324, this PR adds a new `or_default` method to the various `Entry` APIs (currently just for `BTreeMap` and `HashMap`) when `V: Default`. This method is effectively a shorthand for `or_insert_with(Default::default)`.
2 parents 8fc0fc8 + 9389d26 commit 817e1b8

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `entry_or_default`
2+
3+
The tracking issue for this feature is: [#44324]
4+
5+
[#44324]: https://github.com/rust-lang/rust/issues/44324
6+
7+
------------------------
8+
9+
The `entry_or_default` feature adds a new method to `hash_map::Entry`
10+
and `btree_map::Entry`, `or_default`, when `V: Default`. This method is
11+
semantically identical to `or_insert_with(Default::default)`, and will
12+
insert the default value for the type if no entry exists for the current
13+
key.

src/liballoc/btree/map.rs

+27
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,33 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
21042104
}
21052105
}
21062106

2107+
impl<'a, K: Ord, V: Default> Entry<'a, K, V> {
2108+
#[unstable(feature = "entry_or_default", issue = "44324")]
2109+
/// Ensures a value is in the entry by inserting the default value if empty,
2110+
/// and returns a mutable reference to the value in the entry.
2111+
///
2112+
/// # Examples
2113+
///
2114+
/// ```
2115+
/// #![feature(entry_or_default)]
2116+
/// # fn main() {
2117+
/// use std::collections::BTreeMap;
2118+
///
2119+
/// let mut map: BTreeMap<&str, Option<usize>> = BTreeMap::new();
2120+
/// map.entry("poneyland").or_default();
2121+
///
2122+
/// assert_eq!(map["poneyland"], None);
2123+
/// # }
2124+
/// ```
2125+
pub fn or_default(self) -> &'a mut V {
2126+
match self {
2127+
Occupied(entry) => entry.into_mut(),
2128+
Vacant(entry) => entry.insert(Default::default()),
2129+
}
2130+
}
2131+
2132+
}
2133+
21072134
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
21082135
/// Gets a reference to the key that would be used when inserting a value
21092136
/// through the VacantEntry.

src/libstd/collections/hash/map.rs

+27
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,33 @@ impl<'a, K, V> Entry<'a, K, V> {
20012001
}
20022002
}
20032003

2004+
impl<'a, K, V: Default> Entry<'a, K, V> {
2005+
#[unstable(feature = "entry_or_default", issue = "44324")]
2006+
/// Ensures a value is in the entry by inserting the default value if empty,
2007+
/// and returns a mutable reference to the value in the entry.
2008+
///
2009+
/// # Examples
2010+
///
2011+
/// ```
2012+
/// #![feature(entry_or_default)]
2013+
/// # fn main() {
2014+
/// use std::collections::HashMap;
2015+
///
2016+
/// let mut map: HashMap<&str, Option<u32>> = HashMap::new();
2017+
/// map.entry("poneyland").or_default();
2018+
///
2019+
/// assert_eq!(map["poneyland"], None);
2020+
/// # }
2021+
/// ```
2022+
pub fn or_default(self) -> &'a mut V {
2023+
match self {
2024+
Occupied(entry) => entry.into_mut(),
2025+
Vacant(entry) => entry.insert(Default::default()),
2026+
}
2027+
}
2028+
2029+
}
2030+
20042031
impl<'a, K, V> OccupiedEntry<'a, K, V> {
20052032
/// Gets a reference to the key in the entry.
20062033
///

0 commit comments

Comments
 (0)