Skip to content

Commit 250444e

Browse files
committed
Use arrays instead of Vecs in documentation examples
This change is ported from rust-lang/rust#91861. Also finish porting rust-lang/rust#84111, partially ported here in sekineh#34, by including the example of constructing a binary heap from an array.
1 parent 5c40e51 commit 250444e

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/binary_heap.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ use std::vec;
225225
/// assert!(heap.is_empty())
226226
/// ```
227227
///
228+
/// A `BinaryHeap` with a known list of items can be initialized from an array:
229+
///
230+
/// ```
231+
/// use binary_heap_plus::BinaryHeap;
232+
///
233+
/// // This will create a max-heap.
234+
/// let heap = BinaryHeap::from([1, 5, 2]);
235+
/// ```
236+
///
228237
/// ## Min-heap
229238
///
230239
/// `BinaryHeap` can also act as a min-heap without requiring [`Reverse`] or a custom [`Ord`]
@@ -685,7 +694,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
685694
///
686695
/// // replace the comparor
687696
/// heap.replace_cmp(Comparator { ascending: false });
688-
/// assert_eq!(heap.into_iter_sorted().collect::<Vec<_>>(), vec![5, 3, 1]);
697+
/// assert_eq!(heap.into_iter_sorted().collect::<Vec<_>>(), [5, 3, 1]);
689698
/// ```
690699
#[inline]
691700
pub fn replace_cmp(&mut self, cmp: C) {
@@ -755,7 +764,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
755764
///
756765
/// ```
757766
/// use binary_heap_plus::BinaryHeap;
758-
/// let mut heap = BinaryHeap::from(vec![1, 3]);
767+
/// let mut heap = BinaryHeap::from([1, 3]);
759768
///
760769
/// assert_eq!(heap.pop(), Some(3));
761770
/// assert_eq!(heap.pop(), Some(1));
@@ -828,7 +837,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
828837
/// ```
829838
/// use binary_heap_plus::BinaryHeap;
830839
///
831-
/// let mut heap = BinaryHeap::from(vec![1, 2, 4, 5, 7]);
840+
/// let mut heap = BinaryHeap::from([1, 2, 4, 5, 7]);
832841
/// heap.push(6);
833842
/// heap.push(3);
834843
///
@@ -1057,8 +1066,8 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
10571066
/// ```
10581067
/// use binary_heap_plus::BinaryHeap;
10591068
///
1060-
/// let mut a = BinaryHeap::from(vec![-10, 1, 2, 3, 3]);
1061-
/// let mut b = BinaryHeap::from(vec![-20, 5, 43]);
1069+
/// let mut a = BinaryHeap::from([-10, 1, 2, 3, 3]);
1070+
/// let mut b = BinaryHeap::from([-20, 5, 43]);
10621071
///
10631072
/// a.append(&mut b);
10641073
///
@@ -1089,7 +1098,7 @@ impl<T, C> BinaryHeap<T, C> {
10891098
///
10901099
/// ```
10911100
/// use binary_heap_plus::BinaryHeap;
1092-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
1101+
/// let heap = BinaryHeap::from([1, 2, 3, 4]);
10931102
///
10941103
/// // Print 1, 2, 3, 4 in arbitrary order
10951104
/// for x in heap.iter() {
@@ -1112,9 +1121,9 @@ impl<T, C> BinaryHeap<T, C> {
11121121
///
11131122
/// ```
11141123
/// use binary_heap_plus::BinaryHeap;
1115-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
1124+
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5]);
11161125
///
1117-
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
1126+
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
11181127
/// ```
11191128
// #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
11201129
pub fn into_iter_sorted(self) -> IntoIterSorted<T, C> {
@@ -1268,7 +1277,7 @@ impl<T, C> BinaryHeap<T, C> {
12681277
///
12691278
/// ```
12701279
/// use binary_heap_plus::BinaryHeap;
1271-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
1280+
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]);
12721281
/// let vec = heap.into_vec();
12731282
///
12741283
/// // Will print in some order
@@ -1290,7 +1299,7 @@ impl<T, C> BinaryHeap<T, C> {
12901299
///
12911300
/// ```
12921301
/// use binary_heap_plus::BinaryHeap;
1293-
/// let heap = BinaryHeap::from(vec![1, 3]);
1302+
/// let heap = BinaryHeap::from([1, 3]);
12941303
///
12951304
/// assert_eq!(heap.len(), 2);
12961305
/// ```
@@ -1337,7 +1346,7 @@ impl<T, C> BinaryHeap<T, C> {
13371346
///
13381347
/// ```
13391348
/// use binary_heap_plus::BinaryHeap;
1340-
/// let mut heap = BinaryHeap::from(vec![1, 3]);
1349+
/// let mut heap = BinaryHeap::from([1, 3]);
13411350
///
13421351
/// assert!(!heap.is_empty());
13431352
///
@@ -1363,7 +1372,7 @@ impl<T, C> BinaryHeap<T, C> {
13631372
///
13641373
/// ```
13651374
/// use binary_heap_plus::BinaryHeap;
1366-
/// let mut heap = BinaryHeap::from(vec![1, 3]);
1375+
/// let mut heap = BinaryHeap::from([1, 3]);
13671376
///
13681377
/// assert!(!heap.is_empty());
13691378
///
@@ -1693,7 +1702,7 @@ impl<T, C> IntoIterator for BinaryHeap<T, C> {
16931702
///
16941703
/// ```
16951704
/// use binary_heap_plus::BinaryHeap;
1696-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
1705+
/// let heap = BinaryHeap::from([1, 2, 3, 4]);
16971706
///
16981707
/// // Print 1, 2, 3, 4 in arbitrary order
16991708
/// for x in heap.into_iter() {

0 commit comments

Comments
 (0)