@@ -58,6 +58,74 @@ use self::Entry::*;
5858/// It is a logic error for a key to be modified in such a way that the key's ordering relative to
5959/// any other key, as determined by the `Ord` trait, changes while it is in the map. This is
6060/// normally only possible through `Cell`, `RefCell`, global state, I/O, or unsafe code.
61+ ///
62+ /// # Examples
63+ ///
64+ /// ```
65+ /// use std::collections::BTreeMap;
66+ ///
67+ /// // type inference lets us omit an explicit type signature (which
68+ /// // would be `BTreeMap<&str, &str>` in this example).
69+ /// let mut movie_reviews = BTreeMap::new();
70+ ///
71+ /// // review some books.
72+ /// movie_reviews.insert("Office Space", "Deals with real issues in the workplace.");
73+ /// movie_reviews.insert("Pulp Fiction", "Masterpiece.");
74+ /// movie_reviews.insert("The Godfather", "Very enjoyable.");
75+ /// movie_reviews.insert("The Blues Brothers", "Eye lyked it alot.");
76+ ///
77+ /// // check for a specific one.
78+ /// if !movie_reviews.contains_key("Les Misérables") {
79+ /// println!("We've got {} reviews, but Les Misérables ain't one.",
80+ /// movie_reviews.len());
81+ /// }
82+ ///
83+ /// // oops, this review has a lot of spelling mistakes, let's delete it.
84+ /// movie_reviews.remove("The Blues Brothers");
85+ ///
86+ /// // look up the values associated with some keys.
87+ /// let to_find = ["Up!", "Office Space"];
88+ /// for book in &to_find {
89+ /// match movie_reviews.get(book) {
90+ /// Some(review) => println!("{}: {}", book, review),
91+ /// None => println!("{} is unreviewed.", book)
92+ /// }
93+ /// }
94+ ///
95+ /// // iterate over everything.
96+ /// for (movie, review) in &movie_reviews {
97+ /// println!("{}: \"{}\"", movie, review);
98+ /// }
99+ /// ```
100+ ///
101+ /// `BTreeMap` also implements an [`Entry API`](#method.entry), which allows
102+ /// for more complex methods of getting, setting, updating and removing keys and
103+ /// their values:
104+ ///
105+ /// ```
106+ /// use std::collections::BTreeMap;
107+ ///
108+ /// // type inference lets us omit an explicit type signature (which
109+ /// // would be `BTreeMap<&str, u8>` in this example).
110+ /// let mut player_stats = BTreeMap::new();
111+ ///
112+ /// fn random_stat_buff() -> u8 {
113+ /// // could actually return some random value here - let's just return
114+ /// // some fixed value for now
115+ /// 42
116+ /// }
117+ ///
118+ /// // insert a key only if it doesn't already exist
119+ /// player_stats.entry("health").or_insert(100);
120+ ///
121+ /// // insert a key using a function that provides a new value only if it
122+ /// // doesn't already exist
123+ /// player_stats.entry("defence").or_insert_with(random_stat_buff);
124+ ///
125+ /// // update a key, guarding against the key possibly not being set
126+ /// let stat = player_stats.entry("attack").or_insert(100);
127+ /// *stat += random_stat_buff();
128+ /// ```
61129#[ stable( feature = "rust1" , since = "1.0.0" ) ]
62130pub struct BTreeMap < K , V > {
63131 root : node:: Root < K , V > ,
@@ -276,6 +344,19 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
276344
277345impl < K : Ord , V > BTreeMap < K , V > {
278346 /// Makes a new empty BTreeMap with a reasonable choice for B.
347+ ///
348+ /// # Examples
349+ ///
350+ /// Basic usage:
351+ ///
352+ /// ```
353+ /// use std::collections::BTreeMap;
354+ ///
355+ /// let mut map = BTreeMap::new();
356+ ///
357+ /// // entries can now be inserted into the empty map
358+ /// a.insert(1, "a");
359+ /// ```
279360 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
280361 pub fn new ( ) -> BTreeMap < K , V > {
281362 BTreeMap {
@@ -288,6 +369,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
288369 ///
289370 /// # Examples
290371 ///
372+ /// Basic usage:
373+ ///
291374 /// ```
292375 /// use std::collections::BTreeMap;
293376 ///
@@ -309,6 +392,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
309392 ///
310393 /// # Examples
311394 ///
395+ /// Basic usage:
396+ ///
312397 /// ```
313398 /// use std::collections::BTreeMap;
314399 ///
@@ -332,6 +417,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
332417 ///
333418 /// # Examples
334419 ///
420+ /// Basic usage:
421+ ///
335422 /// ```
336423 /// use std::collections::BTreeMap;
337424 ///
@@ -352,6 +439,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
352439 ///
353440 /// # Examples
354441 ///
442+ /// Basic usage:
443+ ///
355444 /// ```
356445 /// use std::collections::BTreeMap;
357446 ///
@@ -384,6 +473,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
384473 ///
385474 /// # Examples
386475 ///
476+ /// Basic usage:
477+ ///
387478 /// ```
388479 /// use std::collections::BTreeMap;
389480 ///
@@ -414,6 +505,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
414505 ///
415506 /// # Examples
416507 ///
508+ /// Basic usage:
509+ ///
417510 /// ```
418511 /// use std::collections::BTreeMap;
419512 ///
@@ -443,6 +536,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
443536 ///
444537 /// # Examples
445538 ///
539+ /// Basic usage:
540+ ///
446541 /// ```
447542 /// #![feature(btree_range, collections_bound)]
448543 ///
@@ -516,6 +611,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
516611 ///
517612 /// # Examples
518613 ///
614+ /// Basic usage:
615+ ///
519616 /// ```
520617 /// #![feature(btree_range, collections_bound)]
521618 ///
@@ -591,6 +688,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
591688 ///
592689 /// # Examples
593690 ///
691+ /// Basic usage:
692+ ///
594693 /// ```
595694 /// use std::collections::BTreeMap;
596695 ///
@@ -1199,6 +1298,8 @@ impl<K, V> BTreeMap<K, V> {
11991298 ///
12001299 /// # Examples
12011300 ///
1301+ /// Basic usage:
1302+ ///
12021303 /// ```
12031304 /// use std::collections::BTreeMap;
12041305 ///
@@ -1229,6 +1330,8 @@ impl<K, V> BTreeMap<K, V> {
12291330 ///
12301331 /// # Examples
12311332 ///
1333+ /// Basic usage:
1334+ ///
12321335 /// ```
12331336 /// use std::collections::BTreeMap;
12341337 ///
@@ -1262,6 +1365,8 @@ impl<K, V> BTreeMap<K, V> {
12621365 ///
12631366 /// # Examples
12641367 ///
1368+ /// Basic usage:
1369+ ///
12651370 /// ```
12661371 /// use std::collections::BTreeMap;
12671372 ///
@@ -1281,6 +1386,8 @@ impl<K, V> BTreeMap<K, V> {
12811386 ///
12821387 /// # Examples
12831388 ///
1389+ /// Basic usage:
1390+ ///
12841391 /// ```
12851392 /// use std::collections::BTreeMap;
12861393 ///
@@ -1300,6 +1407,8 @@ impl<K, V> BTreeMap<K, V> {
13001407 ///
13011408 /// # Examples
13021409 ///
1410+ /// Basic usage:
1411+ ///
13031412 /// ```
13041413 /// use std::collections::BTreeMap;
13051414 ///
@@ -1317,6 +1426,8 @@ impl<K, V> BTreeMap<K, V> {
13171426 ///
13181427 /// # Examples
13191428 ///
1429+ /// Basic usage:
1430+ ///
13201431 /// ```
13211432 /// use std::collections::BTreeMap;
13221433 ///
0 commit comments