Skip to content

OccupiedEntry::key() gives the new key instead of the old one #169

Closed
@ExpHP

Description

@ExpHP
struct Item { value: i32, id: &'static str }

impl PartialEq for Item {
    fn eq(&self, other: &Item) -> bool { self.value == other.value }
}
impl Eq for Item {}
impl std::hash::Hash for Item {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value.hash(state);
    }
}


fn main() {
    use indexmap::map::{IndexMap as Map, Entry};

    let mut map = Map::new();
    map.insert(Item { value: 3, id: "old" }, 200);
    
    match map.entry(Item { value: 3, id: "new" }) {
        Entry::Vacant(e) => panic!(),
        Entry::Occupied(e) => println!("{}", e.key().id),
    }
}

output:

new

Whereas if you instead use std::collections::hash_map::{HashMap as Map, Entry}; then it prints old. This difference in behavior seems surprising.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions