std-method

BinaryHeap::peek_mut

Obtain a mutable guard for the greatest item. When the guard is normally dropped, the heap restores ordering if needed. Avoid deliberately leaking the guard. Expected stdout (not executed): Some(6) Common mistake: Expecting the edited element to remain at the top regardless of its new priority.

Formal Syntax & Structure

rust syntaxrust
fn peek_mut(&mut self) -> Option<PeekMut<'_, T>>

Working Example

Example Snippetrust
fn main() {    use std::collections::BinaryHeap;    let mut q = BinaryHeap::from([9, 6, 2]);    if let Some(mut top) = q.peek_mut() { *top = 1; }    println!("{:?}", q.peek());}
Rust Reference