heap

Macro heap 

Source
macro_rules! heap {
    (
  $( $key: expr ),* $( , )?
 ) => { ... };
}
Expand description

Creates a BinaryHeap from a list of elements.

The into_heap macro simplifies the creation of a BinaryHeap with initial elements.

§Origin

This collection is reexported from alloc.

§Syntax

The macro can be called with a comma-separated list of elements. A trailing comma is optional.

// BinaryHeap of i32
let heap1 = heap!( 3, 1, 4, 1, 5, 9 );

// BinaryHeap of &str
let heap2 = heap!{ "pear", "apple", "banana" };

// With trailing comma
let heap3 = heap!( 2, 7, 1, 8, );

§Parameters

  • $( $key: expr ),* $( , )? : A comma-separated list of elements to insert into the BinaryHeap. Each element can be of any type that implements the Into< T > trait, where T is the type stored in the BinaryHeap.

§Returns

Returns a BinaryHeap containing all the specified elements. The capacity of the heap is automatically determined based on the number of elements provided.

§Example

Basic usage with integers :

let heap = heap!( 5, 3, 7, 1 );
assert_eq!( heap.peek(), Some( &7 ) ); // The largest value is at the top of the heap