我试图使示例代码尽可能简单
struct Level;
pub struct GameManager<'self>{
lvl: Level,
actors: ~[Actor<'self>]
}
struct Actor<'self>{
lvl: &'self Level
}
impl<'self> GameManager <'self> {
pub fn new() -> GameManager{
GameManager {lvl: Level,actors: ~[]}
}
fn spawn_actor<'r>(&'r self) -> Actor<'r>{
Actor{lvl: &'r self.lvl}
}
}
fn main() {
let mut gm = GameManager::new();
let mut actor1 = gm.spawn_actor();
gm.actors.push(actor1);
}
错误:
/home/maik/source/test.rs:23:4: 23:13 error: cannot borrow `gm.actors` as mutable because it is also borrowed as immutable
/home/maik/source/test.rs:23 gm.actors.push(actor1);
^~~~~~~~~
/home/maik/source/test.rs:21:21: 21:23 note: second borrow of `gm.actors` occurs here
/home/maik/source/test.rs:21 let mut actor1 = gm.spawn_actor();
如您所见,我希望 GameManager 生成一个演员。一个 GameManager 作为一个关卡,我希望所有生成的演员都有一个对 GameManager 关卡的引用。
有人可以向我解释这个错误吗?我如何解决它?