Skip to main content

ExtractIf

Struct ExtractIf 

Source
pub struct ExtractIf<'a, K, V, F, A = Global>
where A: Allocator, F: FnMut(&K, &mut V) -> bool,
{ /* private fields */ }
Expand description

A draining iterator over entries of a HashMap which don’t satisfy the predicate f(&k, &mut v) in arbitrary order. The iterator element type is (K, V).

This struct is created by the extract_if method on HashMap. See its documentation for more.

§Examples

use rune::alloc::HashMap;

let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].try_into()?;

let mut extract_if = map.extract_if(|k, _v| k % 2 != 0);
let mut vec = vec![extract_if.next(), extract_if.next()];

// The `ExtractIf` iterator produces items in arbitrary order, so the
// items must be sorted to test them against a sorted array.
vec.sort_unstable();
assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]);

// It is fused iterator
assert_eq!(extract_if.next(), None);
assert_eq!(extract_if.next(), None);
drop(extract_if);

assert_eq!(map.len(), 1);