1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// Find the key in a map that corresponds to a given value.
/// If no key corresponds to the value, return None.
///
/// # Arguments
/// * `object` - A map of key-value pairs.
/// * `value` - The value to search for.
///
/// # Returns
/// * `Option<K>` - The key that corresponds to the value, or None if no key corresponds to the value.
///
/// # Examples
/// ```rust
/// use lowdash::find_key;
/// let mut map = std::collections::HashMap::new();
/// map.insert("a", 1);
/// map.insert("b", 2);
/// map.insert("c", 3);
/// let result = find_key(&map, 2);
/// assert_eq!(result, Some(&"b"));
/// ```
///
/// ```rust
/// use lowdash::find_key;
/// let mut map = std::collections::HashMap::new();
/// map.insert("a", 1);
/// map.insert("b", 2);
/// map.insert("c", 3);
/// let result = find_key(&map, 4);
/// assert_eq!(result, None);
/// ```
///
/// ```rust
/// use lowdash::find_key;
///
/// #[derive(Debug, PartialEq, Eq, Hash)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// let mut map = std::collections::HashMap::new();
/// map.insert(Person { name: "Alice".to_string(), age: 25 }, "Engineer");
/// map.insert(Person { name: "Bob".to_string(), age: 30 }, "Manager");
/// map.insert(Person { name: "Carol".to_string(), age: 35 }, "Director");
///
/// let result = find_key(&map, "Manager");
/// assert_eq!(result, Some(&Person { name: "Bob".to_string(), age: 30 }));
/// ```
pub fn find_key<K, V>(object: &std::collections::HashMap<K, V>, value: V) -> Option<&K>
where
K: std::cmp::Eq + std::hash::Hash,
V: std::cmp::PartialEq,
{
for (k, v) in object {
if *v == value {
return Some(k);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
#[test]
fn test_find_key() {
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = find_key(&map, 2);
assert_eq!(result, Some(&"b"));
}
#[test]
fn test_find_key_not_found() {
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = find_key(&map, 4);
assert_eq!(result, None);
}
#[test]
fn test_find_key_empty_map() {
let map: HashMap<&str, i32> = HashMap::new();
let result = find_key(&map, 1);
assert_eq!(result, None);
}
#[test]
fn test_find_key_with_struct() {
#[derive(Debug, PartialEq, Eq, Hash)]
struct Person {
name: String,
age: u32,
}
let mut map = HashMap::new();
map.insert(
Person {
name: "Alice".to_string(),
age: 25,
},
"Engineer",
);
map.insert(
Person {
name: "Bob".to_string(),
age: 30,
},
"Manager",
);
map.insert(
Person {
name: "Carol".to_string(),
age: 35,
},
"Director",
);
let result = find_key(&map, "Manager");
assert_eq!(
result,
Some(&Person {
name: "Bob".to_string(),
age: 30
})
);
let not_found = find_key(&map, "Intern");
assert_eq!(not_found, None);
}
}