///
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
/// last element of the map and popping it off. **This perturbs
- /// the postion of what used to be the last element!**
+ /// the position of what used to be the last element!**
///
/// Return `None` if `key` is not in map.
///
///
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
/// last element of the map and popping it off. **This perturbs
- /// the postion of what used to be the last element!**
+ /// the position of what used to be the last element!**
///
/// Return `None` if `key` is not in map.
///
///
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
/// last element of the map and popping it off. **This perturbs
- /// the postion of what used to be the last element!**
+ /// the position of what used to be the last element!**
///
/// Return `None` if `key` is not in map.
///
///
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
/// last element of the map and popping it off. **This perturbs
- /// the postion of what used to be the last element!**
+ /// the position of what used to be the last element!**
///
/// Computes in **O(1)** time (average).
pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
assert_eq!(&mut TestEnum::DefaultValue, map.entry(2).or_default());
}
+ #[test]
+ fn occupied_entry_key() {
+ // These keys match hash and equality, but their addresses are distinct.
+ let (k1, k2) = (&mut 1, &mut 1);
+ let k1_ptr = k1 as *const i32;
+ let k2_ptr = k2 as *const i32;
+ assert_ne!(k1_ptr, k2_ptr);
+
+ let mut map = IndexMap::new();
+ map.insert(k1, "value");
+ match map.entry(k2) {
+ Entry::Occupied(ref e) => {
+ // `OccupiedEntry::key` should reference the key in the map,
+ // not the key that was used to find the entry.
+ let ptr = *e.key() as *const i32;
+ assert_eq!(ptr, k1_ptr);
+ assert_ne!(ptr, k2_ptr);
+ }
+ Entry::Vacant(_) => panic!(),
+ }
+ }
+
#[test]
fn keys() {
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];