Skip to main content

Drain

Struct Drain 

Source
pub struct Drain<'a, K, V, A = Global>
where A: Allocator,
{ /* private fields */ }
Expand description

A draining iterator over the entries of a HashMap in arbitrary order. The iterator element type is (K, V).

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

ยงExamples

use rune::alloc::HashMap;

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

let mut drain_iter = map.drain();
let mut vec = vec![drain_iter.next(), drain_iter.next(), drain_iter.next()];

// The `Drain` 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((2, "b")), Some((3, "c"))]);

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