std-type

BinaryHeap with Reverse — minimum priority first

Wrap priorities in Reverse to make smaller natural values rank higher in a max-heap. Unwrap the returned Reverse value when processing it. Expected stdout (not executed): 5 12 30 Common mistake: Negating arbitrary integer priorities, which can overflow at the signed minimum.

Formal Syntax & Structure

rust syntaxrust
BinaryHeap<Reverse<T>>

Working Example

Example Snippetrust
fn main() {    use std::collections::BinaryHeap;    use std::cmp::Reverse;    let mut times = BinaryHeap::from([Reverse(30), Reverse(5), Reverse(12)]);    while let Some(Reverse(t)) = times.pop() { println!("{t}"); }}
Rust Reference