Tracking Issue for map_try_insert · Issue #82766 · rust-lang/rust (original) (raw)
This is a tracking issue for BTreeMap::try_insert
and HashMap::try_insert
.
Unlike .insert()
, .try_insert()
does not overwrite existing values, and has a meaningful error type with more context. See #82764
// alloc::collections::btree_map
impl<K: Ord, V> BTreeMap<K, V> { pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, OccupiedError<'_, K, V>>; }
pub struct OccupiedError<'a, K: 'a, V: 'a> { pub entry: OccupiedEntry<'a, K, V>, pub value: V, }
impl<K: Debug + Ord, V: Debug> Debug for OccupiedError<'_, K, V>; impl<'a, K: Debug + Ord, V: Debug> Display for OccupiedError<'a, K, V>; impl<'a, K: Debug + Ord, V: Debug> Error for OccupiedError<'a, K, V>;
// std::collections::hash_map
impl<K: Eq + Hash, V, S: BuildHasher> HashMap<K, V, S> { pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, OccupiedError<'_, K, V>>; }
pub struct OccupiedError<'a, K: 'a, V: 'a> { pub entry: OccupiedEntry<'a, K, V>, pub value: V, }
impl<K: Debug, V: Debug> Debug for OccupiedError<'_, K, V>; impl<'a, K: Debug, V: Debug> fmt::Display for OccupiedError<'a, K, V>; impl<'a, K: Debug, V: Debug> Error for OccupiedError<'a, K, V>;