pick_by

Function pick_by 

Source
pub fn pick_by<K, V, F>(map: &HashMap<K, V>, predicate: F) -> HashMap<K, V>
where K: Eq + Hash + Clone, V: Clone, F: Fn(&K, &V) -> bool,
Expand description

Filters a map by applying a predicate to its key-value pairs.

Iterates over each key-value pair in the input map and includes it in the result map only if the predicate returns true for that pair.

§Arguments

  • map - The input map to filter.
  • predicate - A function that takes a key and value, and returns true if the pair should be included.

§Returns

  • HashMap<K, V> - A new map containing all key-value pairs that satisfy the predicate.

§Examples

use lowdash::pick_by;
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

let result = pick_by(&map, |_, v| *v > 1);
assert_eq!(result.len(), 2);
assert!(result.contains_key("b"));
assert!(result.contains_key("c"));