std-type

BinaryHeap<T> — priority queue

Use a heap when the next item should be selected by priority instead of arrival time. With the default ordering, the greatest element is at the top. Ordinary heap updates require T: Ord. Expected stdout (not executed): 9: alarm 4: report 2: mail Common mistake: Assuming equal priorities are processed in arrival order. Tuple ordering compares the second field when the first is equal.

Formal Syntax & Structure

rust syntaxrust
BinaryHeap<T>

Working Example

Example Snippetrust
fn main() {    use std::collections::BinaryHeap;    let mut jobs = BinaryHeap::from([(2, "mail"), (9, "alarm"), (4, "report")]);    while let Some((priority, job)) = jobs.pop() { println!("{priority}: {job}"); }}
Rust Reference