Skip to content

Commit 1e9ce0d

Browse files
author
Ulrik Sverdrup
committed
std: Add example for HashMap::entry()
1 parent 6cd7486 commit 1e9ce0d

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
@@ -916,6 +916,24 @@ impl<K, V, S> HashMap<K, V, S>
916916
}
917917

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

0 commit comments

Comments
 (0)