Skip to content

Commit 35c7943

Browse files
committed
Add or_default to Entry APIs
1 parent 2f1ef9e commit 35c7943

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-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

+29
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,35 @@ 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, String> = BTreeMap::new();
2120+
/// let s = "hoho".to_string();
2121+
///
2122+
/// map.entry("poneyland").or_default();
2123+
///
2124+
/// assert_eq!(map["poneyland"], None);
2125+
/// # }
2126+
/// ```
2127+
pub fn or_default(self) -> &'a mut V {
2128+
match self {
2129+
Occupied(entry) => entry.into_mut(),
2130+
Vacant(entry) => entry.insert(Default::default()),
2131+
}
2132+
}
2133+
2134+
}
2135+
21072136
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
21082137
/// Gets a reference to the key that would be used when inserting a value
21092138
/// through the VacantEntry.

src/libstd/collections/hash/map.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,35 @@ 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<usize>> = HashMap::new();
2017+
/// let s = "hoho".to_string();
2018+
///
2019+
/// map.entry("poneyland").or_default();
2020+
///
2021+
/// assert_eq!(map["poneyland"], None);
2022+
/// # }
2023+
/// ```
2024+
pub fn or_default(self) -> &'a mut V {
2025+
match self {
2026+
Occupied(entry) => entry.into_mut(),
2027+
Vacant(entry) => entry.insert(Default::default()),
2028+
}
2029+
}
2030+
2031+
}
2032+
20042033
impl<'a, K, V> OccupiedEntry<'a, K, V> {
20052034
/// Gets a reference to the key in the entry.
20062035
///

0 commit comments

Comments
 (0)