Rust compilation error,cannot borrow `*self` as mutable because it is also borrowed as immutable

use std::sync::Arc;

use dashmap::{DashMap};

pub struct PData{
pub titalNum:i32,
pub prompt:String,
}

pub struct Data{
pub name:String,
pub age:i32,
pub lData:DashMap<String, PData>
}

impl Data {

pub fn onVailData(&mut self){
    let mut ldata = self.lData.get_mut("liming...lData");
    let mut lldata = ldata.as_mut().unwrap().value_mut();
    lldata.titalNum = 80;
    self.CallData(lldata);
}

pub fn CallData(&mut self, pData:&mut PData){

}

}

fn main() {

let mut tData = Data{name:String::from("liming"), age:30, lData:DashMap::new()};
tData.lData.insert(String::from("liming...lData"), PData { titalNum: 10, prompt: String::from("hi,hi,hi,hi") });
tData.onVailData();

loop {

}

}


why?

To prevent data competition, you can no longer use mutable references if you have already used immutable references in a scope.

let mut ldata = self.lData.get_mut("liming...lData");

//equal
let a= &self.lData;
let mut ldata = a.get_mut("liming...lData");

More generally, you can't call a self-receiving method while holding (or passing) a reference into self if either one are mutable. It's a form of interprocedural conflict -- you can't give away exclusive (&mut) access to all of self if there are outstanding references.

The article has some workarounds.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.