Rust 中的多个参考读者和一个参考作者

Multiple reference readers and one reference writer in Rust

我目前正在开发一个小的 Rust 游戏,从语言开始,基本上有以下代码(我在这里只写了一个最小的例子):

struct Player<'a> {
    pub ship: &'a Ship<'a>,
}

impl<'a> Player<'a> {
    pub fn run(&mut self) {
        // Does some computing with self.ship.x/self.ship.y
    }
}

struct Ship<'a> {
    pub players: Vec<Player<'a>>,
    pub x: f64,
    pub y: f64,
}

impl<'a> Ship<'a> {
    pub fn add_player(&mut self, player: Player<'a>) {
        self.players.push(player);
    }
}

fn main() {
    let mut ship = Ship {
        players: vec![],
        x: 0.0,
        y: 0.0,
    };

    // At some point create a player for the ship
    let player = Player { ship: &ship };
    ship.add_player(player); // <- Forbidden
}

这里最重要的是所有 Player 都可以通过不可变引用访问他们所属的飞船,这样他们就可以轻松访问他们的位置 (x/y)船(随着游戏的运行而随时间变化)。但是,此代码无法编译:

error[E0502]: cannot borrow `ship` as mutable because it is also borrowed as immutable
  --> src/main.rs:32:5
   |
31 |     let player = Player { ship: &ship };
   |                                 ----- immutable borrow occurs here
32 |     ship.add_player(player);
   |     ^^^^^----------^^^^^^^^
   |     |    |
   |     |    immutable borrow later used by call
   |     mutable borrow occurs here

我知道 player 正在借用 ship 作为不可变的并且我仍在尝试在借用发生后修改 ship,但我找不到什么是在这种情况下我应该使用正确的智能指针或包装器吗?您会使用 RwLockRefCell 还是其他?

您的想法是正确的,因为您可能需要使用 RefCell、RwLock 甚至 Rc。但是,这些概念更高级,我不建议您在刚开始学习这门语言时尝试使用它们。相反,我会从 Player 结构中删除 Ship 引用,只让 Ship 包含对 Players 的引用。

如果您还没有阅读过,我强烈推荐 official rust book,这是对语言的很好的介绍和很好的示例!