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 theBinaryHeap. Each element can be of any type that implements theInto< T >trait, whereTis the type stored in theBinaryHeap.
§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