std-method

BinaryHeap::retain

Remove every item for which the predicate returns false. Predicate visit order is unspecified. The remaining queue still follows heap ordering. Expected stdout (not executed): [7, 8] Common mistake: Using a predicate whose correctness depends on visitation order.

Formal Syntax & Structure

rust syntaxrust
fn retain<F>(&mut self, f: F) where F: FnMut(&T) -> bool

Working Example

Example Snippetrust
fn main() {    use std::collections::BinaryHeap;    let mut q = BinaryHeap::from([1, 7, 3, 8]);    q.retain(|p| *p >= 5);    println!("{:?}", q.into_sorted_vec());}
Rust Reference