Skip to content

Commit 6a3524e

Browse files
Ulrik Sverdrupsteveklabnik
Ulrik Sverdrup
authored andcommitted
std: Add example for HashMap::entry()
1 parent 86a1165 commit 6a3524e

File tree

1 file changed

+18
-0
lines changed
  • src/libstd/collections/hash

1 file changed

+18
-0
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,24 @@ impl<K, V, S> HashMap<K, V, S>
917917
}
918918

919919
/// Gets the given key's corresponding entry in the map for in-place manipulation.
920+
///
921+
/// # Examples
922+
///
923+
/// ```
924+
/// use std::collections::HashMap;
925+
///
926+
/// let mut letters = HashMap::new();
927+
///
928+
/// for ch in "a short treatise on fungi".chars() {
929+
/// let counter = letters.entry(ch).or_insert(0);
930+
/// *counter += 1;
931+
/// }
932+
///
933+
/// assert_eq!(letters[&'s'], 2);
934+
/// assert_eq!(letters[&'t'], 3);
935+
/// assert_eq!(letters[&'u'], 1);
936+
/// assert_eq!(letters.get(&'y'), None);
937+
/// ```
920938
#[stable(feature = "rust1", since = "1.0.0")]
921939
pub fn entry(&mut self, key: K) -> Entry<K, V> {
922940
// Gotta resize now.

0 commit comments

Comments
 (0)