<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `BinaryHeap` struct in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, BinaryHeap"><title>std::collections::binary_heap::BinaryHeap - Rust</title><link rel="stylesheet" type="text/css" href="../../../normalize.css"><link rel="stylesheet" type="text/css" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../../dark.css"><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script src="../../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc struct"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Struct BinaryHeap</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#methods">Methods</a><div class="sidebar-links"><a href="#method.new">new</a><a href="#method.with_capacity">with_capacity</a><a href="#method.iter">iter</a><a href="#method.peek">peek</a><a href="#method.peek_mut">peek_mut</a><a href="#method.capacity">capacity</a><a href="#method.reserve_exact">reserve_exact</a><a href="#method.reserve">reserve</a><a href="#method.shrink_to_fit">shrink_to_fit</a><a href="#method.shrink_to">shrink_to</a><a href="#method.pop">pop</a><a href="#method.push">push</a><a href="#method.into_vec">into_vec</a><a href="#method.into_sorted_vec">into_sorted_vec</a><a href="#method.len">len</a><a href="#method.is_empty">is_empty</a><a href="#method.drain">drain</a><a href="#method.clear">clear</a><a href="#method.append">append</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-From%3CVec%3CT%3E%3E">From<Vec<T>></a><a href="#impl-From%3CBinaryHeap%3CT%3E%3E">From<BinaryHeap<T>></a><a href="#impl-FromIterator%3CT%3E">FromIterator<T></a><a href="#impl-Default">Default</a><a href="#impl-Debug">Debug</a><a href="#impl-Clone">Clone</a><a href="#impl-Extend%3C%26%27a%20T%3E">Extend<&'a T></a><a href="#impl-Extend%3CT%3E">Extend<T></a><a href="#impl-IntoIterator">IntoIterator</a></div><a class="sidebar-title" href="#synthetic-implementations">Auto Trait Implementations</a><div class="sidebar-links"><a href="#impl-Send">Send</a><a href="#impl-Sync">Sync</a></div></div><p class='location'><a href='../../index.html'>std</a>::<wbr><a href='../index.html'>collections</a>::<wbr><a href='index.html'>binary_heap</a></p><script>window.sidebarCurrent = {name: 'BinaryHeap', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../../settings.html"><img src="../../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Struct <a href='../../index.html'>std</a>::<wbr><a href='../index.html'>collections</a>::<wbr><a href='index.html'>binary_heap</a>::<wbr><a class="struct" href=''>BinaryHeap</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#221-223' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust struct'>pub struct BinaryHeap<T> { /* fields omitted */ }</pre></div><div class='docblock'><p>A priority queue implemented with a binary heap.</p> <p>This will be a max-heap.</p> <p>It is a logic error for an item to be modified in such a way that the item's ordering relative to any other item, as determined by the <code>Ord</code> trait, changes while it is in the heap. This is normally only possible through <code>Cell</code>, <code>RefCell</code>, global state, I/O, or unsafe code.</p> <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="comment">// Type inference lets us omit an explicit type signature (which</span> <span class="comment">// would be `BinaryHeap<i32>` in this example).</span> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="comment">// We can use peek to look at the next item in the heap. In this case,</span> <span class="comment">// there's no items in there yet so we get None.</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek</span>(), <span class="prelude-val">None</span>); <span class="comment">// Let's add some scores...</span> <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">1</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">5</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">2</span>); <span class="comment">// Now peek shows the most important item in the heap.</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">5</span>)); <span class="comment">// We can check the length of a heap.</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">len</span>(), <span class="number">3</span>); <span class="comment">// We can iterate over the items in the heap, although they are returned in</span> <span class="comment">// a random order.</span> <span class="kw">for</span> <span class="ident">x</span> <span class="kw">in</span> <span class="kw-2">&</span><span class="ident">heap</span> { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">x</span>); } <span class="comment">// If we instead pop these scores, they should come back in order.</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="number">5</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="number">2</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="number">1</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">None</span>); <span class="comment">// We can clear the heap of any remaining items.</span> <span class="ident">heap</span>.<span class="ident">clear</span>(); <span class="comment">// The heap should now be empty.</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">is_empty</span>())<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0A%0A%2F%2F%20Type%20inference%20lets%20us%20omit%20an%20explicit%20type%20signature%20(which%0A%2F%2F%20would%20be%20%60BinaryHeap%3Ci32%3E%60%20in%20this%20example).%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0A%0A%2F%2F%20We%20can%20use%20peek%20to%20look%20at%20the%20next%20item%20in%20the%20heap.%20In%20this%20case%2C%0A%2F%2F%20there's%20no%20items%20in%20there%20yet%20so%20we%20get%20None.%0Aassert_eq!(heap.peek()%2C%20None)%3B%0A%0A%2F%2F%20Let's%20add%20some%20scores...%0Aheap.push(1)%3B%0Aheap.push(5)%3B%0Aheap.push(2)%3B%0A%0A%2F%2F%20Now%20peek%20shows%20the%20most%20important%20item%20in%20the%20heap.%0Aassert_eq!(heap.peek()%2C%20Some(%265))%3B%0A%0A%2F%2F%20We%20can%20check%20the%20length%20of%20a%20heap.%0Aassert_eq!(heap.len()%2C%203)%3B%0A%0A%2F%2F%20We%20can%20iterate%20over%20the%20items%20in%20the%20heap%2C%20although%20they%20are%20returned%20in%0A%2F%2F%20a%20random%20order.%0Afor%20x%20in%20%26heap%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A%0A%2F%2F%20If%20we%20instead%20pop%20these%20scores%2C%20they%20should%20come%20back%20in%20order.%0Aassert_eq!(heap.pop()%2C%20Some(5))%3B%0Aassert_eq!(heap.pop()%2C%20Some(2))%3B%0Aassert_eq!(heap.pop()%2C%20Some(1))%3B%0Aassert_eq!(heap.pop()%2C%20None)%3B%0A%0A%2F%2F%20We%20can%20clear%20the%20heap%20of%20any%20remaining%20items.%0Aheap.clear()%3B%0A%0A%2F%2F%20The%20heap%20should%20now%20be%20empty.%0Aassert!(heap.is_empty())%0A%7D">Run</a></pre> </div> <h2 id='methods' class='small-section-header'> Methods<a href='#methods' class='anchor'></a> </h2> <h3 id='impl' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#309-859' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.new' class="method"><span id='new.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.new' class='fnname'>new</a>() -> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#322-324' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates an empty <code>BinaryHeap</code> as a max-heap.</p> <h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">4</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0Aheap.push(4)%3B%0A%7D">Run</a></pre> </div><h4 id='method.with_capacity' class="method"><span id='with_capacity.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.with_capacity' class='fnname'>with_capacity</a>(capacity: <a class="primitive" href="../../primitive.usize.html">usize</a>) -> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#341-343' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates an empty <code>BinaryHeap</code> with a specific capacity. This preallocates enough memory for <code>capacity</code> elements, so that the <code>BinaryHeap</code> does not have to be reallocated until it contains at least that many values.</p> <h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">4</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Awith_capacity(10)%3B%0Aheap.push(4)%3B%0A%7D">Run</a></pre> </div><h4 id='method.iter' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></h3><code class="content"><span class="where fmt-newline">impl<'a, T> <a class="trait" href="../../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></span><span class="where fmt-newline"> type <a href='../../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../../primitive.reference.html">&'a </a>T;</span></code></div></div><span id='iter.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.iter' class='fnname'>iter</a>(&self) -> <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#362-364' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns an iterator visiting all values in the underlying vector, in arbitrary order.</p> <h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]); <span class="comment">// Print 1, 2, 3, 4 in arbitrary order</span> <span class="kw">for</span> <span class="ident">x</span> <span class="kw">in</span> <span class="ident">heap</span>.<span class="ident">iter</span>() { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">x</span>); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%202%2C%203%2C%204%5D)%3B%0A%0A%2F%2F%20Print%201%2C%202%2C%203%2C%204%20in%20arbitrary%20order%0Afor%20x%20in%20heap.iter()%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A%7D">Run</a></pre> </div><h4 id='method.peek' class="method"><span id='peek.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.peek' class='fnname'>peek</a>(&self) -> <a class="enum" href="../../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="primitive" href="../../primitive.reference.html">&</a>T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#384-386' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns the greatest item in the binary heap, or <code>None</code> if it is empty.</p> <h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek</span>(), <span class="prelude-val">None</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">1</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">5</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">2</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">5</span>)); <a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0Aassert_eq!(heap.peek()%2C%20None)%3B%0A%0Aheap.push(1)%3B%0Aheap.push(5)%3B%0Aheap.push(2)%3B%0Aassert_eq!(heap.peek()%2C%20Some(%265))%3B%0A%7D">Run</a></pre> </div><h4 id='method.peek_mut' class="method"><span id='peek_mut.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.peek_mut' class='fnname'>peek_mut</a>(&mut self) -> <a class="enum" href="../../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="struct" href="../../../std/collections/binary_heap/struct.PeekMut.html" title="struct std::collections::binary_heap::PeekMut">PeekMut</a><T>></code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.12.0'>1.12.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#413-422' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns a mutable reference to the greatest item in the binary heap, or <code>None</code> if it is empty.</p> <p>Note: If the <code>PeekMut</code> value is leaked, the heap may be in an inconsistent state.</p> <h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek_mut</span>().<span class="ident">is_none</span>()); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">1</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">5</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">2</span>); { <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">val</span> <span class="op">=</span> <span class="ident">heap</span>.<span class="ident">peek_mut</span>().<span class="ident">unwrap</span>(); <span class="kw-2">*</span><span class="ident">val</span> <span class="op">=</span> <span class="number">0</span>; } <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">2</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0Aassert!(heap.peek_mut().is_none())%3B%0A%0Aheap.push(1)%3B%0Aheap.push(5)%3B%0Aheap.push(2)%3B%0A%7B%0A%20%20%20%20let%20mut%20val%20%3D%20heap.peek_mut().unwrap()%3B%0A%20%20%20%20*val%20%3D%200%3B%0A%7D%0Aassert_eq!(heap.peek()%2C%20Some(%262))%3B%0A%7D">Run</a></pre> </div><h4 id='method.capacity' class="method"><span id='capacity.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.capacity' class='fnname'>capacity</a>(&self) -> <a class="primitive" href="../../primitive.usize.html">usize</a></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#437-439' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns the number of elements the binary heap can hold without reallocating.</p> <h1 id="examples-6" class="section-header"><a href="#examples-6">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">with_capacity</span>(<span class="number">100</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">100</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">4</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Awith_capacity(100)%3B%0Aassert!(heap.capacity()%20%3E%3D%20100)%3B%0Aheap.push(4)%3B%0A%7D">Run</a></pre> </div><h4 id='method.reserve_exact' class="method"><span id='reserve_exact.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.reserve_exact' class='fnname'>reserve_exact</a>(&mut self, additional: <a class="primitive" href="../../primitive.usize.html">usize</a>)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#466-468' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Reserves the minimum capacity for exactly <code>additional</code> more elements to be inserted in the given <code>BinaryHeap</code>. Does nothing if the capacity is already sufficient.</p> <p>Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer <a href="#method.reserve"><code>reserve</code></a> if future insertions are expected.</p> <h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1> <p>Panics if the new capacity overflows <code>usize</code>.</p> <h1 id="examples-7" class="section-header"><a href="#examples-7">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="ident">heap</span>.<span class="ident">reserve_exact</span>(<span class="number">100</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">100</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">4</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0Aheap.reserve_exact(100)%3B%0Aassert!(heap.capacity()%20%3E%3D%20100)%3B%0Aheap.push(4)%3B%0A%7D">Run</a></pre> </div><h4 id='method.reserve' class="method"><span id='reserve.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.reserve' class='fnname'>reserve</a>(&mut self, additional: <a class="primitive" href="../../primitive.usize.html">usize</a>)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#489-491' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Reserves capacity for at least <code>additional</code> more elements to be inserted in the <code>BinaryHeap</code>. The collection may reserve more space to avoid frequent reallocations.</p> <h1 id="panics-1" class="section-header"><a href="#panics-1">Panics</a></h1> <p>Panics if the new capacity overflows <code>usize</code>.</p> <h1 id="examples-8" class="section-header"><a href="#examples-8">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="ident">heap</span>.<span class="ident">reserve</span>(<span class="number">100</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">100</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">4</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0Aheap.reserve(100)%3B%0Aassert!(heap.capacity()%20%3E%3D%20100)%3B%0Aheap.push(4)%3B%0A%7D">Run</a></pre> </div><h4 id='method.shrink_to_fit' class="method"><span id='shrink_to_fit.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.shrink_to_fit' class='fnname'>shrink_to_fit</a>(&mut self)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#508-510' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Discards as much additional capacity as possible.</p> <h1 id="examples-9" class="section-header"><a href="#examples-9">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span>: <span class="ident">BinaryHeap</span><span class="op"><</span><span class="ident">i32</span><span class="op">></span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">with_capacity</span>(<span class="number">100</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">100</span>); <span class="ident">heap</span>.<span class="ident">shrink_to_fit</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">==</span> <span class="number">0</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%3A%20BinaryHeap%3Ci32%3E%20%3D%20BinaryHeap%3A%3Awith_capacity(100)%3B%0A%0Aassert!(heap.capacity()%20%3E%3D%20100)%3B%0Aheap.shrink_to_fit()%3B%0Aassert!(heap.capacity()%20%3D%3D%200)%3B%0A%7D">Run</a></pre> </div><h4 id='method.shrink_to' class="method"><span id='shrink_to.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.shrink_to' class='fnname'>shrink_to</a>(&mut self, min_capacity: <a class="primitive" href="../../primitive.usize.html">usize</a>)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#533-535' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shrink_to</code>)</summary><p>new API</p> </details></div></div><div class='docblock'><p>Discards capacity with a lower bound.</p> <p>The capacity will remain at least as large as both the length and the supplied value.</p> <p>Panics if the current capacity is smaller than the supplied minimum capacity.</p> <h1 id="examples-10" class="section-header"><a href="#examples-10">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="attribute">#![<span class="ident">feature</span>(<span class="ident">shrink_to</span>)]</span> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span>: <span class="ident">BinaryHeap</span><span class="op"><</span><span class="ident">i32</span><span class="op">></span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">with_capacity</span>(<span class="number">100</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">100</span>); <span class="ident">heap</span>.<span class="ident">shrink_to</span>(<span class="number">10</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">10</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(shrink_to)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%3A%20BinaryHeap%3Ci32%3E%20%3D%20BinaryHeap%3A%3Awith_capacity(100)%3B%0A%0Aassert!(heap.capacity()%20%3E%3D%20100)%3B%0Aheap.shrink_to(10)%3B%0Aassert!(heap.capacity()%20%3E%3D%2010)%3B%0A%7D&version=nightly">Run</a></pre> </div><h4 id='method.pop' class="method"><span id='pop.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.pop' class='fnname'>pop</a>(&mut self) -> <a class="enum" href="../../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#553-561' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Removes the greatest item from the binary heap and returns it, or <code>None</code> if it is empty.</p> <h1 id="examples-11" class="section-header"><a href="#examples-11">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">3</span>]); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="number">3</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="number">1</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">pop</span>(), <span class="prelude-val">None</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%203%5D)%3B%0A%0Aassert_eq!(heap.pop()%2C%20Some(3))%3B%0Aassert_eq!(heap.pop()%2C%20Some(1))%3B%0Aassert_eq!(heap.pop()%2C%20None)%3B%0A%7D">Run</a></pre> </div><h4 id='method.push' class="method"><span id='push.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.push' class='fnname'>push</a>(&mut self, item: T)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#580-584' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Pushes an item onto the binary heap.</p> <h1 id="examples-12" class="section-header"><a href="#examples-12">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">3</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">5</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">1</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">len</span>(), <span class="number">3</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">peek</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">5</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0Aheap.push(3)%3B%0Aheap.push(5)%3B%0Aheap.push(1)%3B%0A%0Aassert_eq!(heap.len()%2C%203)%3B%0Aassert_eq!(heap.peek()%2C%20Some(%265))%3B%0A%7D">Run</a></pre> </div><h4 id='method.into_vec' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></h3><code class="content"><span class="where fmt-newline">impl <a class="trait" href="../../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></span></code></div></div><span id='into_vec.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.into_vec' class='fnname'>into_vec</a>(self) -> <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#604-606' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Consumes the <code>BinaryHeap</code> and returns the underlying vector in arbitrary order.</p> <h1 id="examples-13" class="section-header"><a href="#examples-13">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]); <span class="kw">let</span> <span class="ident">vec</span> <span class="op">=</span> <span class="ident">heap</span>.<span class="ident">into_vec</span>(); <span class="comment">// Will print in some order</span> <span class="kw">for</span> <span class="ident">x</span> <span class="kw">in</span> <span class="ident">vec</span> { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">x</span>); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%2C%207%5D)%3B%0Alet%20vec%20%3D%20heap.into_vec()%3B%0A%0A%2F%2F%20Will%20print%20in%20some%20order%0Afor%20x%20in%20vec%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A%7D">Run</a></pre> </div><h4 id='method.into_sorted_vec' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></h3><code class="content"><span class="where fmt-newline">impl <a class="trait" href="../../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></span></code></div></div><span id='into_sorted_vec.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.into_sorted_vec' class='fnname'>into_sorted_vec</a>(self) -> <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#626-634' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Consumes the <code>BinaryHeap</code> and returns a vector in sorted (ascending) order.</p> <h1 id="examples-14" class="section-header"><a href="#examples-14">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">7</span>]); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">6</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">3</span>); <span class="kw">let</span> <span class="ident">vec</span> <span class="op">=</span> <span class="ident">heap</span>.<span class="ident">into_sorted_vec</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">vec</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0A%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%202%2C%204%2C%205%2C%207%5D)%3B%0Aheap.push(6)%3B%0Aheap.push(3)%3B%0A%0Alet%20vec%20%3D%20heap.into_sorted_vec()%3B%0Aassert_eq!(vec%2C%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%2C%207%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.len' class="method"><span id='len.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.len' class='fnname'>len</a>(&self) -> <a class="primitive" href="../../primitive.usize.html">usize</a></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#725-727' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns the length of the binary heap.</p> <h1 id="examples-15" class="section-header"><a href="#examples-15">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">3</span>]); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">len</span>(), <span class="number">2</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%203%5D)%3B%0A%0Aassert_eq!(heap.len()%2C%202)%3B%0A%7D">Run</a></pre> </div><h4 id='method.is_empty' class="method"><span id='is_empty.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.is_empty' class='fnname'>is_empty</a>(&self) -> <a class="primitive" href="../../primitive.bool.html">bool</a></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#748-750' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Checks if the binary heap is empty.</p> <h1 id="examples-16" class="section-header"><a href="#examples-16">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">is_empty</span>()); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">3</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">5</span>); <span class="ident">heap</span>.<span class="ident">push</span>(<span class="number">1</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">heap</span>.<span class="ident">is_empty</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0A%0Aassert!(heap.is_empty())%3B%0A%0Aheap.push(3)%3B%0Aheap.push(5)%3B%0Aheap.push(1)%3B%0A%0Aassert!(!heap.is_empty())%3B%0A%7D">Run</a></pre> </div><h4 id='method.drain' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.Drain.html" title="struct std::collections::binary_heap::Drain">Drain</a><'a, T></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.Drain.html" title="struct std::collections::binary_heap::Drain">Drain</a><'a, T></h3><code class="content"><span class="where fmt-newline">impl<'a, T> <a class="trait" href="../../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.Drain.html" title="struct std::collections::binary_heap::Drain">Drain</a><'a, T> <span class="where fmt-newline">where<br> T: 'a, </span></span><span class="where fmt-newline"> type <a href='../../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = T;</span></code></div></div><span id='drain.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.drain' class='fnname'>drain</a>(&mut self) -> <a class="struct" href="../../../std/collections/binary_heap/struct.Drain.html" title="struct std::collections::binary_heap::Drain">Drain</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#774-776' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Clears the binary heap, returning an iterator over the removed elements.</p> <p>The elements are removed in arbitrary order.</p> <h1 id="examples-17" class="section-header"><a href="#examples-17">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">3</span>]); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">heap</span>.<span class="ident">is_empty</span>()); <span class="kw">for</span> <span class="ident">x</span> <span class="kw">in</span> <span class="ident">heap</span>.<span class="ident">drain</span>() { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">x</span>); } <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">is_empty</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%203%5D)%3B%0A%0Aassert!(!heap.is_empty())%3B%0A%0Afor%20x%20in%20heap.drain()%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A%0Aassert!(heap.is_empty())%3B%0A%7D">Run</a></pre> </div><h4 id='method.clear' class="method"><span id='clear.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.clear' class='fnname'>clear</a>(&mut self)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#795-797' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Drops all items from the binary heap.</p> <h1 id="examples-18" class="section-header"><a href="#examples-18">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">3</span>]); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">heap</span>.<span class="ident">is_empty</span>()); <span class="ident">heap</span>.<span class="ident">clear</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">heap</span>.<span class="ident">is_empty</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20mut%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%203%5D)%3B%0A%0Aassert!(!heap.is_empty())%3B%0A%0Aheap.clear()%3B%0A%0Aassert!(heap.is_empty())%3B%0A%7D">Run</a></pre> </div><h4 id='method.append' class="method"><span id='append.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.append' class='fnname'>append</a>(&mut self, other: &mut <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T>)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.11.0'>1.11.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#828-858' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Moves all the elements of <code>other</code> into <code>self</code>, leaving <code>other</code> empty.</p> <h1 id="examples-19" class="section-header"><a href="#examples-19">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="op">-</span><span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>]; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="ident">v</span>); <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="op">-</span><span class="number">20</span>, <span class="number">5</span>, <span class="number">43</span>]; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="ident">v</span>); <span class="ident">a</span>.<span class="ident">append</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">b</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">a</span>.<span class="ident">into_sorted_vec</span>(), [<span class="op">-</span><span class="number">20</span>, <span class="op">-</span><span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">43</span>]); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">b</span>.<span class="ident">is_empty</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0A%0Alet%20v%20%3D%20vec!%5B-10%2C%201%2C%202%2C%203%2C%203%5D%3B%0Alet%20mut%20a%20%3D%20BinaryHeap%3A%3Afrom(v)%3B%0A%0Alet%20v%20%3D%20vec!%5B-20%2C%205%2C%2043%5D%3B%0Alet%20mut%20b%20%3D%20BinaryHeap%3A%3Afrom(v)%3B%0A%0Aa.append(%26mut%20b)%3B%0A%0Aassert_eq!(a.into_sorted_vec()%2C%20%5B-20%2C%20-10%2C%201%2C%202%2C%203%2C%203%2C%205%2C%2043%5D)%3B%0Aassert!(b.is_empty())%3B%0A%7D">Run</a></pre> </div></div> <h2 id='implementations' class='small-section-header'> Trait Implementations<a href='#implementations' class='anchor'></a> </h2> <div id='implementations-list'><h3 id='impl-From%3CVec%3CT%3E%3E' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T>> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-From%3CVec%3CT%3E%3E' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1096-1102' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.from' class="method"><span id='from.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(vec: <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T>) -> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1097-1101' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CBinaryHeap%3CT%3E%3E' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T>> for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T></code><a href='#impl-From%3CBinaryHeap%3CT%3E%3E' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1105-1109' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.from-1' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></h3><code class="content"><span class="where fmt-newline">impl <a class="trait" href="../../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../../primitive.u8.html">u8</a>></span></code></div></div><span id='from.v-1' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(heap: <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T>) -> <a class="struct" href="../../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1106-1108' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-FromIterator%3CT%3E' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><T> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-FromIterator%3CT%3E' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1112-1116' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.from_iter' class="method"><span id='from_iter.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(iter: I) -> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = T>, </span></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1113-1115' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates a value from an iterator. <a href="../../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-Default' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/default/trait.Default.html" title="trait std::default::Default">Default</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-Default' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#294-300' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.default' class="method"><span id='default.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/default/trait.Default.html#tymethod.default' class='fnname'>default</a>() -> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#297-299' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates an empty <code>BinaryHeap<T></code>.</p> </div></div><h3 id='impl-Debug' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a> + <a class="trait" href="../../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a>, </span></code><a href='#impl-Debug' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.4.0'>1.4.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#303-307' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.fmt' class="method"><span id='fmt.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class="struct" href="../../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -> <a class="enum" href="../../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../../primitive.unit.html">()</a>, <a class="struct" href="../../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#304-306' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Formats the value using the given formatter. <a href="../../../std/fmt/trait.Debug.html#tymethod.fmt">Read more</a></p> </div></div><h3 id='impl-Clone' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/clone/trait.Clone.html" title="trait std::clone::Clone">Clone</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/clone/trait.Clone.html" title="trait std::clone::Clone">Clone</a>, </span></code><a href='#impl-Clone' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#283-291' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.clone' class="method"><span id='clone.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/clone/trait.Clone.html#tymethod.clone' class='fnname'>clone</a>(&self) -> <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#284-286' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns a copy of the value. <a href="../../../std/clone/trait.Clone.html#tymethod.clone">Read more</a></p> </div><h4 id='method.clone_from' class="method"><span id='clone_from.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&mut self, source: &<a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T>)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#288-290' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Performs copy-assignment from <code>source</code>. <a href="../../../std/clone/trait.Clone.html#method.clone_from">Read more</a></p> </div></div><h3 id='impl-Extend%3C%26%27a%20T%3E' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<'a, T> <a class="trait" href="../../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><<a class="primitive" href="../../primitive.reference.html">&'a </a>T> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: 'a + <a class="trait" href="../../../std/marker/trait.Copy.html" title="trait std::marker::Copy">Copy</a> + <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-Extend%3C%26%27a%20T%3E' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1192-1196' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.extend' class="method"><span id='extend.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="primitive" href="../../primitive.reference.html">&'a </a>T>, </span></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1193-1195' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-Extend%3CT%3E' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><T> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-Extend%3CT%3E' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1159-1164' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.extend-1' class="method"><span id='extend.v-1' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = T>, </span></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1161-1163' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-IntoIterator' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-IntoIterator' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1119-1144' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='associatedtype.Item' class="type"><span id='Item.t' class='invisible'><code>type <a href='../../../std/iter/trait.IntoIterator.html#associatedtype.Item' class="type">Item</a> = T</code></span></h4> <div class='docblock'><p>The type of the elements being iterated over.</p> </div><h4 id='associatedtype.IntoIter' class="type"><span id='IntoIter.t' class='invisible'><code>type <a href='../../../std/iter/trait.IntoIterator.html#associatedtype.IntoIter' class="type">IntoIter</a> = <a class="struct" href="../../../std/collections/binary_heap/struct.IntoIter.html" title="struct std::collections::binary_heap::IntoIter">IntoIter</a><T></code></span></h4> <div class='docblock'><p>Which kind of iterator are we turning this into?</p> </div><h4 id='method.into_iter' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.IntoIter.html" title="struct std::collections::binary_heap::IntoIter">IntoIter</a><T></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.IntoIter.html" title="struct std::collections::binary_heap::IntoIter">IntoIter</a><T></h3><code class="content"><span class="where fmt-newline">impl<T> <a class="trait" href="../../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.IntoIter.html" title="struct std::collections::binary_heap::IntoIter">IntoIter</a><T></span><span class="where fmt-newline"> type <a href='../../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = T;</span></code></div></div><span id='into_iter.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/iter/trait.IntoIterator.html#tymethod.into_iter' class='fnname'>into_iter</a>(self) -> <a class="struct" href="../../../std/collections/binary_heap/struct.IntoIter.html" title="struct std::collections::binary_heap::IntoIter">IntoIter</a><T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1141-1143' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates a consuming iterator, that is, one that moves each value out of the binary heap in arbitrary order. The binary heap cannot be used after calling this.</p> <h1 id="examples-20" class="section-header"><a href="#examples-20">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>; <span class="kw">let</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">from</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]); <span class="comment">// Print 1, 2, 3, 4 in arbitrary order</span> <span class="kw">for</span> <span class="ident">x</span> <span class="kw">in</span> <span class="ident">heap</span>.<span class="ident">into_iter</span>() { <span class="comment">// x has type i32, not &i32</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">x</span>); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Alet%20heap%20%3D%20BinaryHeap%3A%3Afrom(vec!%5B1%2C%202%2C%203%2C%204%5D)%3B%0A%0A%2F%2F%20Print%201%2C%202%2C%203%2C%204%20in%20arbitrary%20order%0Afor%20x%20in%20heap.into_iter()%20%7B%0A%20%20%20%20%2F%2F%20x%20has%20type%20i32%2C%20not%20%26i32%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A%7D">Run</a></pre> </div></div><h3 id='impl-IntoIterator-1' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<'a, T> <a class="trait" href="../../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a> for &'a <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code><a href='#impl-IntoIterator-1' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1147-1156' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='associatedtype.Item-1' class="type"><span id='Item.t-1' class='invisible'><code>type <a href='../../../std/iter/trait.IntoIterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../../primitive.reference.html">&'a </a>T</code></span></h4> <div class='docblock'><p>The type of the elements being iterated over.</p> </div><h4 id='associatedtype.IntoIter-1' class="type"><span id='IntoIter.t-1' class='invisible'><code>type <a href='../../../std/iter/trait.IntoIterator.html#associatedtype.IntoIter' class="type">IntoIter</a> = <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></code></span></h4> <div class='docblock'><p>Which kind of iterator are we turning this into?</p> </div><h4 id='method.into_iter-1' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></h3><code class="content"><span class="where fmt-newline">impl<'a, T> <a class="trait" href="../../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></span><span class="where fmt-newline"> type <a href='../../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../../primitive.reference.html">&'a </a>T;</span></code></div></div><span id='into_iter.v-1' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../../std/iter/trait.IntoIterator.html#tymethod.into_iter' class='fnname'>into_iter</a>(self) -> <a class="struct" href="../../../std/collections/binary_heap/struct.Iter.html" title="struct std::collections::binary_heap::Iter">Iter</a><'a, T></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../../src/alloc/binary_heap.rs.html#1153-1155' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates an iterator from a value. <a href="../../../std/iter/trait.IntoIterator.html#tymethod.into_iter">Read more</a></p> </div></div></div> <h2 id='synthetic-implementations' class='small-section-header'> Auto Trait Implementations<a href='#synthetic-implementations' class='anchor'></a> </h2> <div id='synthetic-implementations-list'> <h3 id='impl-Send' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a>, </span></code><a href='#impl-Send' class='anchor'></a></span></td><td><span class='out-of-band'></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-Sync' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl<T> <a class="trait" href="../../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a> for <a class="struct" href="../../../std/collections/binary_heap/struct.BinaryHeap.html" title="struct std::collections::binary_heap::BinaryHeap">BinaryHeap</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a>, </span></code><a href='#impl-Sync' class='anchor'></a></span></td><td><span class='out-of-band'></span></td></tr></tbody></table></h3><div class='impl-items'></div></div></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../../";window.currentCrate = "std";</script><script src="../../../aliases.js"></script><script src="../../../main.js"></script><script defer src="../../../search-index.js"></script></body></html>