Will the borrow checker ever be able to handle this simple loop with a mutable HashMap logic?

@SkiFire13 indeed, you are perfectly right.
There is something that makes the do_some_loops_KO safe and the other one unsafe, but my assumption is not what makes the difference.
Still thinking about it though! :wink:

The problem is that this optimization depends on the internals of HashMap. Using shared references doesn't mean that HashMap isn't modifying its internal data structures. It could, by using Mutexes or Cells. Shared doesn't mean read-only.

Something like this will perform at most 3 lookups, regardless of the number of loop iterations:

fn do_some_loops_KO(maps: &mut HashMap<String, RefCell<HashMap<String, String>>>) {
    let inner = match maps.get("key") {
        Some(v) => v,
        None => {
            maps.insert("key".to_owned(), Default::default());

    for _ in 0..10 {
        let (k, v) = use_maps(maps);
        inner.borrow_mut().insert(k, v);