@@ -58,6 +58,74 @@ use self::Entry::*;
58
58
/// It is a logic error for a key to be modified in such a way that the key's ordering relative to
59
59
/// any other key, as determined by the `Ord` trait, changes while it is in the map. This is
60
60
/// 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
+ /// ```
61
129
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
62
130
pub struct BTreeMap < K , V > {
63
131
root : node:: Root < K , V > ,
@@ -276,6 +344,14 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
276
344
277
345
impl < K : Ord , V > BTreeMap < K , V > {
278
346
/// Makes a new empty BTreeMap with a reasonable choice for B.
347
+ ///
348
+ /// # Examples
349
+ ///
350
+ /// ```
351
+ /// use std::collections::BTreeMap;
352
+ ///
353
+ /// let mut map: BTreeMap<&str, isize> = BTreeMap::new();
354
+ /// ```
279
355
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
280
356
pub fn new ( ) -> BTreeMap < K , V > {
281
357
BTreeMap {
0 commit comments