pick_by_values

Function pick_by_values 

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

Filters a map by selecting only the specified values.

Iterates over each key-value pair in the input map and includes it in the result map only if the value is present in the provided values slice.

§Arguments

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

§Returns

  • HashMap<K, V> - A new map containing only the key-value pairs that have values present in the values slice.

§Examples

use lowdash::pick_by_values;
use std::collections::HashMap;

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

let values = vec![1, 3];
let result = pick_by_values(&map, &values);
assert_eq!(result.len(), 2);
assert!(result.contains_key("a"));
assert!(result.contains_key("c"));