I'm writing the "ArrayMap" object that includes the set of key and Vec in Rust. The following are the module and the test code.
use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
pub struct ArrayMap {
map: RefCell<HashMap<String, Vec<i32>>>,
}
impl ArrayMap {
pub fn new() -> Self {
let map = RefCell::new(HashMap::<String, Vec<i32>>::new());
ArrayMap{map: map}
}
pub fn add(&self, key: &str) {
self.map.borrow_mut().insert(key.to_string(), Vec::<i32>::new());
}
pub fn get(&self, key: &str) -> RefMut<Vec<i32>> {
let map = self.map.borrow_mut(); // panic
RefMut::map(map, |map| map.get_mut(key).unwrap())
}
}
mod array_map;
use array_map::ArrayMap;
fn main() {
let m = ArrayMap::new();
m.add("array0");
m.add("array1");
let _a0 = m.get("array0");
let _a1 = m.get("array1");
}
However, the error below occurs.
thread 'main' panicked at 'already borrowed: BorrowMutError', src\array_map.rs:19:28
I know it works if I divide two m.get() into other blocks but I don't want to do so for the actual usage.
And I tried to use Rc<RefCell> for the "map" of ArrayMap.
use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
use std::rc::Rc;
pub struct ArrayMap {
map: Rc<RefCell<HashMap<String, Vec<i32>>>>,
}
impl ArrayMap {
pub fn new() -> Self {
let map = Rc::new(RefCell::new(HashMap::<String, Vec<i32>>::new()));
ArrayMap{map: map}
}
pub fn add(&self, key: &str) {
self.map.borrow_mut().insert(key.to_string(), Vec::<i32>::new());
}
pub fn get(&self, key: &str) -> RefMut<Vec<i32>> {
let map = Rc::clone(&self.map).borrow_mut();
RefMut::map(map, |map| map.get_mut(key).unwrap()) // error[E0515]
}
}
But this error occurs.
error[E0515]: cannot return value referencing temporary value
--> src\array_map.rs:21:9
Could you please tell me the way to avoid this situation? Any ideas are welcome.