about summary refs log tree commit diff
diff options
context:
space:
mode:
authorjbranchaud <jbranchaud@gmail.com>2014-12-22 11:29:31 -0600
committerjbranchaud <jbranchaud@gmail.com>2014-12-22 12:13:06 -0600
commite8fcbfb9599cb04808ad7241ad140ac7c961b280 (patch)
tree02a3684df0ee65d2dddb6f725a6d7e0512d46bdf
parent34d680009205de2302b902d8f9f5f7ae7a042f1a (diff)
Add a doctest for the btreemap's entry method.
Make some updates to the entry doctest.
-rw-r--r--src/libcollections/btree/map.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs
index 01096c1fd4e..5fb963b15ce 100644
--- a/src/libcollections/btree/map.rs
+++ b/src/libcollections/btree/map.rs
@@ -1288,6 +1288,30 @@ impl<K, V> BTreeMap<K, V> {
 
 impl<K: Ord, V> BTreeMap<K, V> {
     /// Gets the given key's corresponding entry in the map for in-place manipulation.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::BTreeMap;
+    /// use std::collections::btree_map::Entry;
+    ///
+    /// let mut count: BTreeMap<&str, uint> = BTreeMap::new();
+    ///
+    /// // count the number of occurrences of letters in the vec
+    /// for x in vec!["a","b","a","c","a","b"].iter() {
+    ///     match count.entry(*x) {
+    ///         Entry::Vacant(view) => {
+    ///             view.set(1);
+    ///         },
+    ///         Entry::Occupied(mut view) => {
+    ///             let v = view.get_mut();
+    ///             *v += 1;
+    ///         },
+    ///     }
+    /// }
+    ///
+    /// assert_eq!(count["a"], 3u);
+    /// ```
     pub fn entry<'a>(&'a mut self, mut key: K) -> Entry<'a, K, V> {
         // same basic logic of `swap` and `pop`, blended together
         let mut stack = stack::PartialSearchStack::new(self);