This code will report an error :sys
does not live long enough E0597 borrowed value does not live long enough in sys.find_mut_env(env_id);
let application = match SIMULATION_APPLICATION.write() {
Ok(mut sys) => {
println!("Enable env {}", env_id);
let option = sys.find_mut_env(env_id);
option
}
Err(_) => None
};
and this is SimulationApplication:
#[derive(Debug, Clone)]
pub struct SimulationApplication {
pub envs: HashMap<i32, Environment>,
}
impl SimulationApplication {
pub fn new() -> SimulationApplication {
SimulationApplication {
envs: HashMap::new(),
}
}
pub fn find_envs(&self) -> Vec<&Environment> {
self.envs.values().collect::<Vec<_>>()
}
pub fn find_env(&self, id: i32) -> Option<&Environment> {
self.envs.get(&id)
}
pub fn find_mut_env(&mut self, id: i32) -> Option<&mut Environment> {
println!("find_mut_env: {}", id);
self.envs.get_mut(&id)
}
}
lazy_static! {
pub static ref SIMULATION_APPLICATION: RwLock<SimulationApplication> = {
let simualtion = RwLock::new(SimulationApplication::new());
simualtion.write().unwrap().envs.insert(1, Environment {
id: 1,
name: "dev",
state: 0,
address: "localhost",
db_prop: DbProp {
host: "localhost",
port: 3306,
user: "root",
password: "root",
},
mq_prop: MqttProp {
host: "localhost",
port: 1883,
user: "root",
password: "root",
},
robot_db_pool: None,
robots: HashMap::new(),
}
);
simualtion
};
}
how can i fix it