Skip to content

Commit e8fcbfb

Browse files
committed
Add a doctest for the btreemap's entry method.
Make some updates to the entry doctest.
1 parent 34d6800 commit e8fcbfb

File tree

1 file changed

+24
-0
lines changed
  • src/libcollections/btree

1 file changed

+24
-0
lines changed

src/libcollections/btree/map.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,30 @@ impl<K, V> BTreeMap<K, V> {
12881288

12891289
impl<K: Ord, V> BTreeMap<K, V> {
12901290
/// Gets the given key's corresponding entry in the map for in-place manipulation.
1291+
///
1292+
/// # Examples
1293+
///
1294+
/// ```
1295+
/// use std::collections::BTreeMap;
1296+
/// use std::collections::btree_map::Entry;
1297+
///
1298+
/// let mut count: BTreeMap<&str, uint> = BTreeMap::new();
1299+
///
1300+
/// // count the number of occurrences of letters in the vec
1301+
/// for x in vec!["a","b","a","c","a","b"].iter() {
1302+
/// match count.entry(*x) {
1303+
/// Entry::Vacant(view) => {
1304+
/// view.set(1);
1305+
/// },
1306+
/// Entry::Occupied(mut view) => {
1307+
/// let v = view.get_mut();
1308+
/// *v += 1;
1309+
/// },
1310+
/// }
1311+
/// }
1312+
///
1313+
/// assert_eq!(count["a"], 3u);
1314+
/// ```
12911315
pub fn entry<'a>(&'a mut self, mut key: K) -> Entry<'a, K, V> {
12921316
// same basic logic of `swap` and `pop`, blended together
12931317
let mut stack = stack::PartialSearchStack::new(self);

0 commit comments

Comments
 (0)