Rust code is error

use dashmap::{DashMap};

pub struct Data{
     pub name:String,
     pub age:i32,

pub trait baseHouse {
    fn getData(&mut self, uid:String) ->&mut Data;
    fn addData(&mut self, uid:String, data:Data);

pub  struct  romeHouse{
    pub  BattleTime:f32,
    pub  sceneId:String,
    pub  Person:DashMap<String, Data>

impl baseHouse for romeHouse{
        fn getData(&mut self, uid:String) ->&mut Data{
            let mut pt = self.Person.get_mut(uid.as_str());
            let outData = pt.unwrap().value_mut();

    fn addData(&mut self, uid:String, data:Data){
        self.Person.insert(uid, data);

fn  main() {

    let mut roomServer = romeHouse{BattleTime:1.0, sceneId:String::from("741852"), Person:DashMap::new()};
    roomServer.addData(String::from("123"), Data { name: String::from("pin"), age:20 });

    loop {


error[8515]: cannot return value referencing temporary value
  --> src\
26 |         let outData = pt.unwrap().value_mut();
   |                       ----------- temporary value created here
27 |         outData
   |         ------- returns a value referencing data omed by the current function

For more inforn•ation about this error, try `rustc --explain 8515`
warning: `rustTest` (bin "rustTest") generated 3 warnings
error: could not compile `rustTest` due to previous error; 3 warnings enitted

What is wrong and right?

Format your code properly using code blocks:

Also, don't post images. Instead, copy the cargo build output into a code block.

1 Like

You can't return references from DashMap. This type doesn't allow that. It doesn't return normal references, it returns a temporary RefMut lock.

If you try to return &mut Data without the RefMut lock, you're bypassing the lock, and this is unsafe.

You can only return RefMut from the function, because once Data is in the DashMap, it can't be used in a function that doesn't have RefMut for it.

1 Like