Skip to content

Improve get_index_mut documentation wrt mutable keys #174

Closed
@TethysSvensson

Description

@TethysSvensson

The function get_index_mut gives a mutable reference to the of an entry, but does not update the corresponding hash. This means you can write buggy code like this:

use indexmap::IndexMap;

fn main() {
    let mut map = IndexMap::new();
    map.insert("foo", 0);
    *map.get_index_mut(0).unwrap().0 = "bar";

    assert!(!map.contains_key("foo"));
    assert!(!map.contains_key("bar"));
    assert!(map.keys().collect::<Vec<_>>() == [&"bar"]);
}

This does not seem like it is the intended behavior, since e.g. first_mut only gives a non-mutable reference to the key.

If this is the intended behavior, then there should probably be a warning in the documentation that you should not changing the key in ways which alter the Hash or Eq behavior.

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