Skip to content

Commit 118b975

Browse files
committed
Add missing documentation examples for BinaryHeap.
As part of the ongoing effort to document all methods with examples, this commit adds the missing examples for the `BinaryHeap` collection type. This is part of issue rust-lang#29348.
1 parent 8f0479b commit 118b975

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/libcollections/binary_heap.rs

+106
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,49 @@ use vec::{self, Vec};
167167
/// item's ordering relative to any other item, as determined by the `Ord`
168168
/// trait, changes while it is in the heap. This is normally only possible
169169
/// 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+
/// ```
170213
#[stable(feature = "rust1", since = "1.0.0")]
171214
pub struct BinaryHeap<T> {
172215
data: Vec<T>,
@@ -331,6 +374,17 @@ impl<T: Ord> BinaryHeap<T> {
331374
}
332375

333376
/// Discards as much additional capacity as possible.
377+
///
378+
/// # Examples
379+
///
380+
/// ```
381+
/// use std::collections::BinaryHeap;
382+
/// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
383+
///
384+
/// assert!(heap.capacity() >= 100);
385+
/// heap.shrink_to_fit();
386+
/// assert!(heap.capacity() == 0);
387+
/// ```
334388
#[stable(feature = "rust1", since = "1.0.0")]
335389
pub fn shrink_to_fit(&mut self) {
336390
self.data.shrink_to_fit();
@@ -571,12 +625,36 @@ impl<T: Ord> BinaryHeap<T> {
571625
}
572626

573627
/// Returns the length of the binary heap.
628+
///
629+
/// # Examples
630+
///
631+
/// ```
632+
/// use std::collections::BinaryHeap;
633+
/// let mut heap = BinaryHeap::from(vec![1, 3]);
634+
///
635+
/// assert_eq!(heap.len(), 2);
636+
/// ```
574637
#[stable(feature = "rust1", since = "1.0.0")]
575638
pub fn len(&self) -> usize {
576639
self.data.len()
577640
}
578641

579642
/// Checks if the binary heap is empty.
643+
///
644+
/// # Examples
645+
///
646+
/// ```
647+
/// use std::collections::BinaryHeap;
648+
/// let mut heap = BinaryHeap::new();
649+
///
650+
/// assert!(heap.is_empty());
651+
///
652+
/// heap.push(3);
653+
/// heap.push(5);
654+
/// heap.push(1);
655+
///
656+
/// assert!(!heap.is_empty());
657+
/// ```
580658
#[stable(feature = "rust1", since = "1.0.0")]
581659
pub fn is_empty(&self) -> bool {
582660
self.len() == 0
@@ -585,13 +663,41 @@ impl<T: Ord> BinaryHeap<T> {
585663
/// Clears the binary heap, returning an iterator over the removed elements.
586664
///
587665
/// The elements are removed in arbitrary order.
666+
///
667+
/// # Examples
668+
///
669+
/// ```
670+
/// use std::collections::BinaryHeap;
671+
/// let mut heap = BinaryHeap::from(vec![1, 3]);
672+
///
673+
/// assert!(!heap.is_empty());
674+
///
675+
/// for x in heap.drain() {
676+
/// println!("{}", x);
677+
/// }
678+
///
679+
/// assert!(heap.is_empty());
680+
/// ```
588681
#[inline]
589682
#[stable(feature = "drain", since = "1.6.0")]
590683
pub fn drain(&mut self) -> Drain<T> {
591684
Drain { iter: self.data.drain(..) }
592685
}
593686

594687
/// Drops all items from the binary heap.
688+
///
689+
/// # Examples
690+
///
691+
/// ```
692+
/// use std::collections::BinaryHeap;
693+
/// let mut heap = BinaryHeap::from(vec![1, 3]);
694+
///
695+
/// assert!(!heap.is_empty());
696+
///
697+
/// heap.clear();
698+
///
699+
/// assert!(heap.is_empty());
700+
/// ```
595701
#[stable(feature = "rust1", since = "1.0.0")]
596702
pub fn clear(&mut self) {
597703
self.drain();

0 commit comments

Comments
 (0)