Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting two mutable references in rust for HashMap [duplicate]

Tags:

rust

I am trying to get started with Rust, and wondered how the following piece of c++ code can be converted to rust.

Consider this C++ code:

void func(int c) {
  unordered_map<int, int> map;
  auto& a = map[10];
  auto& b = map[20];
  // some complex logic
  if (c > 10) {
    a += 1;
    b += 1;
  } else {
    a += 2;
    b += 2;
  }
  cout << map[10] << " " << map[20] << '\n';
}

Right now, I have replaced some complex logic with a simple if/else, but in essence, I need two mutable references to two values inside my hash map.

Consider the corresponding rust code:

fn func(c: i32) {
    let mut map = HashMap::new();
    let a = map.entry(&10).or_insert(0);
    let b = map.entry(&20).or_insert(0);
    // some complex code here
    if c > 10 {
        *a += 1;
        *b += 1;
    } else {
        *a += 2;
        *b += 2;
    }
}

rustc will not compile this code because I have two mutable references to map here.

How should one go about doing this in rust?

like image 555
skgbanga Avatar asked Nov 06 '25 02:11

skgbanga


1 Answers

You could try to use interior mutability pattern to achieve this goal, your code will change to something like this.

use std::collections::HashMap;
use std::cell::Cell;

fn func(c: i32) {
    let mut map = HashMap::new();
    map.insert(10, Cell::new(0));
    map.insert(20, Cell::new(0));
    
    let a = map.get(&10).unwrap();
    let b = map.get(&20).unwrap();
    // some complex code here
    if c > 10 {
        a.set(a.get() + 1);
        b.set(b.get() + 1);
    } else {
        a.set(a.get() + 2);
        b.set(b.get() + 2);
    }
}

Playground link

like image 198
Inline Avatar answered Nov 08 '25 18:11

Inline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!