diff options
| author | Ulrik Sverdrup <root@localhost> | 2015-05-09 13:27:23 +0200 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-05-13 02:58:09 -0400 |
| commit | 6a3524ea2ee52a5aa5c4465f1cf9460dff4c4e04 (patch) | |
| tree | fc8161f9646e969f5f34c6f0da7fa35c3fdffc11 /src/libstd | |
| parent | 86a1165f846f4f370c80de39f8b0bc59ac1da787 (diff) | |
| download | rust-6a3524ea2ee52a5aa5c4465f1cf9460dff4c4e04.tar.gz rust-6a3524ea2ee52a5aa5c4465f1cf9460dff4c4e04.zip | |
std: Add example for HashMap::entry()
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 65d32106342..f5da5f0bf69 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -917,6 +917,24 @@ impl<K, V, S> HashMap<K, V, S> } /// Gets the given key's corresponding entry in the map for in-place manipulation. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashMap; + /// + /// let mut letters = HashMap::new(); + /// + /// for ch in "a short treatise on fungi".chars() { + /// let counter = letters.entry(ch).or_insert(0); + /// *counter += 1; + /// } + /// + /// assert_eq!(letters[&'s'], 2); + /// assert_eq!(letters[&'t'], 3); + /// assert_eq!(letters[&'u'], 1); + /// assert_eq!(letters.get(&'y'), None); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn entry(&mut self, key: K) -> Entry<K, V> { // Gotta resize now. |
