find_key_by

Function find_key_by 

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

Find the key in a map that satisfies a predicate based on both key and value. If no key satisfies the predicate, return None.

§Arguments

  • object - A map of key-value pairs.
  • predicate - A function that takes a key and value and returns a boolean.

§Returns

  • Option<&K> - The key that satisfies the predicate, or None if no key satisfies the predicate.

§Examples

use lowdash::find_key_by;
let mut map = std::collections::HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = find_key_by(&map, |k, v| *k == "b" && *v == 2);
assert_eq!(result, Some(&"b"));
use lowdash::find_key_by;
let mut map = std::collections::HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = find_key_by(&map, |_, v| *v > 2);
assert_eq!(result, Some(&"c"));