我的所有状态都有一个哈希图,它是 a HashMap<String, Rc<State>>
,我想调用当前状态的 member fn init(&mut self)
。但我收到以下代码错误:
...
if let Some(state) = self.states.get_mut(state_id) {
(*state).init();
}
...
这是错误:
src/main.rs:70:25: 70:33 error: cannot borrow immutable borrowed content as mutable
src/main.rs:70 (*state).shutdown();`
从文档来看,问题在于get_mut
返回对状态的可变引用,而不是对可变状态的引用。那么我如何获得对可变状态的引用呢?