Skip to content

Commit cd4c1a5

Browse files
ya7010cuviper
authored andcommitted
feat: add IndexMap::get_key_value_mut
1 parent 48a98b7 commit cd4c1a5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/map.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,22 @@ where
902902
}
903903
}
904904

905+
/// Return references to the key-value pair stored for `key`,
906+
/// if it is present, else `None`.
907+
///
908+
/// Computes in **O(1)** time (average).
909+
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
910+
where
911+
Q: ?Sized + Hash + Equivalent<K>,
912+
{
913+
if let Some(i) = self.get_index_of(key) {
914+
let entry = &mut self.as_entries_mut()[i];
915+
Some((&entry.key, &mut entry.value))
916+
} else {
917+
None
918+
}
919+
}
920+
905921
pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)>
906922
where
907923
Q: ?Sized + Hash + Equivalent<K>,

0 commit comments

Comments
 (0)