]> git.lizzy.rs Git - rust.git/commitdiff
std: Add example for HashMap::entry()
authorUlrik Sverdrup <root@localhost>
Sat, 9 May 2015 11:27:23 +0000 (13:27 +0200)
committerUlrik Sverdrup <root@localhost>
Sat, 9 May 2015 11:27:23 +0000 (13:27 +0200)
src/libstd/collections/hash/map.rs

index eedda3cf4371adf80ce14bbe9aeeeb0e4a6cedc8..ae75de2086b7770821d846594f971bdb66195d7a 100644 (file)
@@ -916,6 +916,24 @@ pub fn iter_mut(&mut self) -> IterMut<K, V> {
     }
 
     /// 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.