I tried to make the example code as simple as possible
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);
}
Error:
/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();
As you can see I want the GameManager to spawn an actor. A GameManager as a Level and I want all spawned actors to have a reference to the GameManager's Level.
Can someone explain this error to me? How do I fix it?