std-method

BinaryHeap::drain

Remove all entries through a draining iterator; the heap retains its allocation for reuse. The order of yielded items is unspecified. Expected stdout (not executed): 10 true Common mistake: Assuming drain produces sorted items.

Formal Syntax & Structure

rust syntaxrust
fn drain(&mut self) -> Drain<'_, T>

Working Example

Example Snippetrust
fn main() {    use std::collections::BinaryHeap;    let mut q = BinaryHeap::from([2, 7, 1]);    let total: i32 = q.drain().sum();    println!("{total} {}", q.is_empty());}
Rust Reference