@@ -167,6 +167,49 @@ use vec::{self, Vec};
167
167
/// item's ordering relative to any other item, as determined by the `Ord`
168
168
/// trait, changes while it is in the heap. This is normally only possible
169
169
/// through `Cell`, `RefCell`, global state, I/O, or unsafe code.
170
+ ///
171
+ /// # Examples
172
+ ///
173
+ /// ```
174
+ /// use std::collections::BinaryHeap;
175
+ ///
176
+ /// // type inference lets us omit an explicit type signature (which
177
+ /// // would be `BinaryHeap<i32>` in this example).
178
+ /// let mut heap = BinaryHeap::new();
179
+ ///
180
+ /// // We can use peek to look at the next item in the heap. In this case,
181
+ /// // there's no items in there yet so we get None.
182
+ /// assert_eq!(heap.peek(), None);
183
+ ///
184
+ /// // Let's add some scores...
185
+ /// heap.push(1);
186
+ /// heap.push(5);
187
+ /// heap.push(2);
188
+ ///
189
+ /// // Now peek shows the most important item in the heap.
190
+ /// assert_eq!(heap.peek(), Some(&5));
191
+ ///
192
+ /// // We can check the length of a heap.
193
+ /// assert_eq!(heap.len(), 3);
194
+ ///
195
+ /// // We can iterate over the items in the heap, although they are returned in
196
+ /// // a random order.
197
+ /// for x in heap.iter() {
198
+ /// println!("{}", x);
199
+ /// }
200
+ ///
201
+ /// // If we instead pop these scores, they should come back in order.
202
+ /// assert_eq!(heap.pop(), Some(5));
203
+ /// assert_eq!(heap.pop(), Some(2));
204
+ /// assert_eq!(heap.pop(), Some(1));
205
+ /// assert_eq!(heap.pop(), None);
206
+ ///
207
+ /// // We can clear the heap of any remaining items.
208
+ /// heap.clear();
209
+ ///
210
+ /// // The heap should now be empty.
211
+ /// assert!(heap.is_empty())
212
+ /// ```
170
213
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
171
214
pub struct BinaryHeap < T > {
172
215
data : Vec < T > ,
@@ -203,6 +246,8 @@ impl<T: Ord> BinaryHeap<T> {
203
246
///
204
247
/// # Examples
205
248
///
249
+ /// Basic usage:
250
+ ///
206
251
/// ```
207
252
/// use std::collections::BinaryHeap;
208
253
/// let mut heap = BinaryHeap::new();
@@ -220,6 +265,8 @@ impl<T: Ord> BinaryHeap<T> {
220
265
///
221
266
/// # Examples
222
267
///
268
+ /// Basic usage:
269
+ ///
223
270
/// ```
224
271
/// use std::collections::BinaryHeap;
225
272
/// let mut heap = BinaryHeap::with_capacity(10);
@@ -235,6 +282,8 @@ impl<T: Ord> BinaryHeap<T> {
235
282
///
236
283
/// # Examples
237
284
///
285
+ /// Basic usage:
286
+ ///
238
287
/// ```
239
288
/// use std::collections::BinaryHeap;
240
289
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
@@ -253,6 +302,8 @@ impl<T: Ord> BinaryHeap<T> {
253
302
///
254
303
/// # Examples
255
304
///
305
+ /// Basic usage:
306
+ ///
256
307
/// ```
257
308
/// use std::collections::BinaryHeap;
258
309
/// let mut heap = BinaryHeap::new();
@@ -273,6 +324,8 @@ impl<T: Ord> BinaryHeap<T> {
273
324
///
274
325
/// # Examples
275
326
///
327
+ /// Basic usage:
328
+ ///
276
329
/// ```
277
330
/// use std::collections::BinaryHeap;
278
331
/// let mut heap = BinaryHeap::with_capacity(100);
@@ -297,6 +350,8 @@ impl<T: Ord> BinaryHeap<T> {
297
350
///
298
351
/// # Examples
299
352
///
353
+ /// Basic usage:
354
+ ///
300
355
/// ```
301
356
/// use std::collections::BinaryHeap;
302
357
/// let mut heap = BinaryHeap::new();
@@ -318,6 +373,8 @@ impl<T: Ord> BinaryHeap<T> {
318
373
///
319
374
/// # Examples
320
375
///
376
+ /// Basic usage:
377
+ ///
321
378
/// ```
322
379
/// use std::collections::BinaryHeap;
323
380
/// let mut heap = BinaryHeap::new();
@@ -331,6 +388,19 @@ impl<T: Ord> BinaryHeap<T> {
331
388
}
332
389
333
390
/// Discards as much additional capacity as possible.
391
+ ///
392
+ /// # Examples
393
+ ///
394
+ /// Basic usage:
395
+ ///
396
+ /// ```
397
+ /// use std::collections::BinaryHeap;
398
+ /// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
399
+ ///
400
+ /// assert!(heap.capacity() >= 100);
401
+ /// heap.shrink_to_fit();
402
+ /// assert!(heap.capacity() == 0);
403
+ /// ```
334
404
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
335
405
pub fn shrink_to_fit ( & mut self ) {
336
406
self . data . shrink_to_fit ( ) ;
@@ -341,6 +411,8 @@ impl<T: Ord> BinaryHeap<T> {
341
411
///
342
412
/// # Examples
343
413
///
414
+ /// Basic usage:
415
+ ///
344
416
/// ```
345
417
/// use std::collections::BinaryHeap;
346
418
/// let mut heap = BinaryHeap::from(vec![1, 3]);
@@ -364,6 +436,8 @@ impl<T: Ord> BinaryHeap<T> {
364
436
///
365
437
/// # Examples
366
438
///
439
+ /// Basic usage:
440
+ ///
367
441
/// ```
368
442
/// use std::collections::BinaryHeap;
369
443
/// let mut heap = BinaryHeap::new();
@@ -386,6 +460,8 @@ impl<T: Ord> BinaryHeap<T> {
386
460
///
387
461
/// # Examples
388
462
///
463
+ /// Basic usage:
464
+ ///
389
465
/// ```
390
466
/// #![feature(binary_heap_extras)]
391
467
///
@@ -424,6 +500,8 @@ impl<T: Ord> BinaryHeap<T> {
424
500
///
425
501
/// # Examples
426
502
///
503
+ /// Basic usage:
504
+ ///
427
505
/// ```
428
506
/// #![feature(binary_heap_extras)]
429
507
///
@@ -454,6 +532,8 @@ impl<T: Ord> BinaryHeap<T> {
454
532
///
455
533
/// # Examples
456
534
///
535
+ /// Basic usage:
536
+ ///
457
537
/// ```
458
538
/// use std::collections::BinaryHeap;
459
539
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
@@ -474,6 +554,8 @@ impl<T: Ord> BinaryHeap<T> {
474
554
///
475
555
/// # Examples
476
556
///
557
+ /// Basic usage:
558
+ ///
477
559
/// ```
478
560
/// use std::collections::BinaryHeap;
479
561
///
@@ -571,12 +653,40 @@ impl<T: Ord> BinaryHeap<T> {
571
653
}
572
654
573
655
/// Returns the length of the binary heap.
656
+ ///
657
+ /// # Examples
658
+ ///
659
+ /// Basic usage:
660
+ ///
661
+ /// ```
662
+ /// use std::collections::BinaryHeap;
663
+ /// let heap = BinaryHeap::from(vec![1, 3]);
664
+ ///
665
+ /// assert_eq!(heap.len(), 2);
666
+ /// ```
574
667
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
575
668
pub fn len ( & self ) -> usize {
576
669
self . data . len ( )
577
670
}
578
671
579
672
/// Checks if the binary heap is empty.
673
+ ///
674
+ /// # Examples
675
+ ///
676
+ /// Basic usage:
677
+ ///
678
+ /// ```
679
+ /// use std::collections::BinaryHeap;
680
+ /// let mut heap = BinaryHeap::new();
681
+ ///
682
+ /// assert!(heap.is_empty());
683
+ ///
684
+ /// heap.push(3);
685
+ /// heap.push(5);
686
+ /// heap.push(1);
687
+ ///
688
+ /// assert!(!heap.is_empty());
689
+ /// ```
580
690
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
581
691
pub fn is_empty ( & self ) -> bool {
582
692
self . len ( ) == 0
@@ -585,13 +695,45 @@ impl<T: Ord> BinaryHeap<T> {
585
695
/// Clears the binary heap, returning an iterator over the removed elements.
586
696
///
587
697
/// The elements are removed in arbitrary order.
698
+ ///
699
+ /// # Examples
700
+ ///
701
+ /// Basic usage:
702
+ ///
703
+ /// ```
704
+ /// use std::collections::BinaryHeap;
705
+ /// let mut heap = BinaryHeap::from(vec![1, 3]);
706
+ ///
707
+ /// assert!(!heap.is_empty());
708
+ ///
709
+ /// for x in heap.drain() {
710
+ /// println!("{}", x);
711
+ /// }
712
+ ///
713
+ /// assert!(heap.is_empty());
714
+ /// ```
588
715
#[ inline]
589
716
#[ stable( feature = "drain" , since = "1.6.0" ) ]
590
717
pub fn drain ( & mut self ) -> Drain < T > {
591
718
Drain { iter : self . data . drain ( ..) }
592
719
}
593
720
594
721
/// Drops all items from the binary heap.
722
+ ///
723
+ /// # Examples
724
+ ///
725
+ /// Basic usage:
726
+ ///
727
+ /// ```
728
+ /// use std::collections::BinaryHeap;
729
+ /// let mut heap = BinaryHeap::from(vec![1, 3]);
730
+ ///
731
+ /// assert!(!heap.is_empty());
732
+ ///
733
+ /// heap.clear();
734
+ ///
735
+ /// assert!(heap.is_empty());
736
+ /// ```
595
737
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
596
738
pub fn clear ( & mut self ) {
597
739
self . drain ( ) ;
@@ -809,6 +951,8 @@ impl<T: Ord> IntoIterator for BinaryHeap<T> {
809
951
///
810
952
/// # Examples
811
953
///
954
+ /// Basic usage:
955
+ ///
812
956
/// ```
813
957
/// use std::collections::BinaryHeap;
814
958
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
0 commit comments