我目前正在开发一个小型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
或其他名称?
您的想法正确,因为您可能需要使用RefCell,RwLock甚至Rc。但是,这些概念更为高级,我不建议您在刚开始学习该语言时尝试使用它们。相反,我将从Player结构中删除Ship引用,只让Ship包含对Player的引用。
如果您还没有的话,我强烈推荐official rust book,它是C语言的很好的介绍,并附有很多示例!