Skip to content

Commit

Permalink
Add HashMap::try_insert and hash_map::OccupiedError.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Mar 4, 2021
1 parent 09cbcdc commit f6fe24a
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions library/std/src/collections/hash/map.rs
Expand Up @@ -842,6 +842,40 @@ where
self.base.insert(k, v)
}

/// Tries to insert a key-value pair into the map, and returns
/// a mutable reference to the value in the entry.
///
/// If the map already had this key present, nothing is updated, and
/// an error containing the occupied entry and the value is returned.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(map_try_insert)]
///
/// use std::collections::HashMap;
///
/// let mut map = HashMap::new();
/// assert_eq!(map.try_insert(37, "a").unwrap(), &"a");
///
/// let err = map.try_insert(37, "b").unwrap_err();
/// assert_eq!(err.entry.key(), &37);
/// assert_eq!(err.entry.get(), &"a");
/// assert_eq!(err.value, "b");
/// ```
#[unstable(feature = "map_try_insert", issue = "none")]
pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, OccupiedError<'_, K, V>>
where
K: Ord,
{
match self.entry(key) {
Occupied(entry) => Err(OccupiedError { entry, value }),
Vacant(entry) => Ok(entry.insert(value)),
}
}

/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
///
Expand Down Expand Up @@ -1851,6 +1885,18 @@ impl<K: Debug, V> Debug for VacantEntry<'_, K, V> {
}
}

/// The error returned by [`try_insert`](HashMap::try_insert) when the key already exists.
///
/// Contains the occupied entry, and the value that was not inserted.
#[unstable(feature = "map_try_insert", issue = "none")]
#[derive(Debug)]
pub struct OccupiedError<'a, K: 'a, V: 'a> {
/// The entry in the map that was already occupied.
pub entry: OccupiedEntry<'a, K, V>,
/// The value which was not inserted, because the entry was already occupied.
pub value: V,
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {
type Item = (&'a K, &'a V);
Expand Down

0 comments on commit f6fe24a

Please sign in to comment.