@@ -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,19 @@ 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
+ /// 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
+ /// ```
279
360
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
280
361
pub fn new ( ) -> BTreeMap < K , V > {
281
362
BTreeMap {
@@ -288,6 +369,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
288
369
///
289
370
/// # Examples
290
371
///
372
+ /// Basic usage:
373
+ ///
291
374
/// ```
292
375
/// use std::collections::BTreeMap;
293
376
///
@@ -309,6 +392,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
309
392
///
310
393
/// # Examples
311
394
///
395
+ /// Basic usage:
396
+ ///
312
397
/// ```
313
398
/// use std::collections::BTreeMap;
314
399
///
@@ -332,6 +417,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
332
417
///
333
418
/// # Examples
334
419
///
420
+ /// Basic usage:
421
+ ///
335
422
/// ```
336
423
/// use std::collections::BTreeMap;
337
424
///
@@ -352,6 +439,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
352
439
///
353
440
/// # Examples
354
441
///
442
+ /// Basic usage:
443
+ ///
355
444
/// ```
356
445
/// use std::collections::BTreeMap;
357
446
///
@@ -384,6 +473,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
384
473
///
385
474
/// # Examples
386
475
///
476
+ /// Basic usage:
477
+ ///
387
478
/// ```
388
479
/// use std::collections::BTreeMap;
389
480
///
@@ -414,6 +505,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
414
505
///
415
506
/// # Examples
416
507
///
508
+ /// Basic usage:
509
+ ///
417
510
/// ```
418
511
/// use std::collections::BTreeMap;
419
512
///
@@ -443,6 +536,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
443
536
///
444
537
/// # Examples
445
538
///
539
+ /// Basic usage:
540
+ ///
446
541
/// ```
447
542
/// #![feature(btree_range, collections_bound)]
448
543
///
@@ -516,6 +611,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
516
611
///
517
612
/// # Examples
518
613
///
614
+ /// Basic usage:
615
+ ///
519
616
/// ```
520
617
/// #![feature(btree_range, collections_bound)]
521
618
///
@@ -591,6 +688,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
591
688
///
592
689
/// # Examples
593
690
///
691
+ /// Basic usage:
692
+ ///
594
693
/// ```
595
694
/// use std::collections::BTreeMap;
596
695
///
@@ -1199,6 +1298,8 @@ impl<K, V> BTreeMap<K, V> {
1199
1298
///
1200
1299
/// # Examples
1201
1300
///
1301
+ /// Basic usage:
1302
+ ///
1202
1303
/// ```
1203
1304
/// use std::collections::BTreeMap;
1204
1305
///
@@ -1229,6 +1330,8 @@ impl<K, V> BTreeMap<K, V> {
1229
1330
///
1230
1331
/// # Examples
1231
1332
///
1333
+ /// Basic usage:
1334
+ ///
1232
1335
/// ```
1233
1336
/// use std::collections::BTreeMap;
1234
1337
///
@@ -1262,6 +1365,8 @@ impl<K, V> BTreeMap<K, V> {
1262
1365
///
1263
1366
/// # Examples
1264
1367
///
1368
+ /// Basic usage:
1369
+ ///
1265
1370
/// ```
1266
1371
/// use std::collections::BTreeMap;
1267
1372
///
@@ -1281,6 +1386,8 @@ impl<K, V> BTreeMap<K, V> {
1281
1386
///
1282
1387
/// # Examples
1283
1388
///
1389
+ /// Basic usage:
1390
+ ///
1284
1391
/// ```
1285
1392
/// use std::collections::BTreeMap;
1286
1393
///
@@ -1300,6 +1407,8 @@ impl<K, V> BTreeMap<K, V> {
1300
1407
///
1301
1408
/// # Examples
1302
1409
///
1410
+ /// Basic usage:
1411
+ ///
1303
1412
/// ```
1304
1413
/// use std::collections::BTreeMap;
1305
1414
///
@@ -1317,6 +1426,8 @@ impl<K, V> BTreeMap<K, V> {
1317
1426
///
1318
1427
/// # Examples
1319
1428
///
1429
+ /// Basic usage:
1430
+ ///
1320
1431
/// ```
1321
1432
/// use std::collections::BTreeMap;
1322
1433
///
0 commit comments