pick_by_keys

Function pick_by_keys 

Source
pub fn pick_by_keys<K, V>(map: &HashMap<K, V>, keys: &[K]) -> HashMap<K, V>
where K: Eq + Hash + Clone, V: Clone,
Expand description

Filters a map by selecting only the specified keys.

Iterates over the provided keys and includes the key-value pair in the result map only if the key exists in the input map.

§Arguments

  • map - The input map to filter.
  • keys - A slice of keys to select from the map.

§Returns

  • HashMap<K, V> - A new map containing only the specified key-value pairs.

§Examples

use lowdash::pick_by_keys;
use std::collections::HashMap;

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

let keys = vec!["a", "c", "d"];
let result = pick_by_keys(&map, &keys);
assert_eq!(result.len(), 2);
assert!(result.contains_key("a"));
assert!(result.contains_key("c"));