109. BinaryHeap::peek_mut — Edit the Top Without Pop-and-Push
Updating the largest element in a BinaryHeap shouldn’t take two heap operations. peek_mut lets you mutate it in place and re-heapifies on drop — one sift instead of two.
The pop-and-push dance
Say you keep tasks in a max-heap by priority and you need to bump the top one down. The naive recipe is to pop, change it, and push it back:
| |
That’s two O(log n) heap operations — a sift-down for pop, a sift-up for push — plus an awkward two-step where the heap briefly forgets it ever had a max.
peek_mut does it in one shot
peek_mut returns a PeekMut guard that derefs to the top element. You mutate it in place, and the heap fixes itself with a single sift-down when the guard is dropped:
| |
One traversal, no temporary owned value, and the heap invariant is restored before the next line of code runs.
Conditionally pop without a re-peek
PeekMut also has an associated pop function. Look at the top, decide whether to keep or remove it, all without peeking twice:
| |
Same shape as Vec::pop_if, but for the heap’s max — pull the top out only when it actually meets your condition.
When to reach for it
Any time you’d write pop().push() to edit the max, peek_mut is the better tool: one heap fixup instead of two, and the borrow stays inside the heap so you don’t shuffle ownership for nothing. Great fit for priority queues that adjust the top entry — schedulers, event loops, top-k trackers.