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

问题描述 投票:1回答:1

我目前正在开发一个小型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,但是我找不到应该用于的正确的智能指针或包装器这种情况?您会使用RwLock还是RefCell或其他名称?

rust reference borrow-checker ownership
1个回答
0
投票

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

如果您还没有的话,我强烈推荐official rust book,它是C语言的很好的介绍,并附有很多示例!

© www.soinside.com 2019 - 2024. All rights reserved.