I'm trying to learn Rust, and as you can imagine, the borrow checker is my biggest adversary. So here's my setup, it's a kind of crate for the game battleship. The game is based on the Battlefield
struct, which consists of Cell
s. A Cell
can reference a Ship
and a Ship
has a vector of all Cell
s it's referenced by, so it's a bi-directional read-only relationship.
pub struct Battlefield<'a> {
cells: Vec<Vec<Cell<'a>>>,
}
#[derive(Debug, PartialEq)]
pub struct Cell<'a> {
ship: Option<&'a Ship<'a>>
}
#[derive(Debug, PartialEq)]
pub struct Ship<'a> {
length: usize,
cells: Vec<&'a Cell<'a>>,
}
My problem is Battlefield
's place_ship
function:
impl<'a> Battlefield<'a> {
pub fn place_ship(&mut self,
ship: &'a mut Ship,
x: usize,
y: usize,
orientation: Orientation)
-> PlaceResult {
// check ship placement in bounds
// check affected cells are free
// set cells' ship ref to ship
// add cell refs to ship's cells field
}
}
It makes sense to me and I don't think there's an ownership problem here, but I'm wrong it seems:
#[cfg(test)]
mod tests {
use super::{Battlefield, X, Y};
use super::Orientation::*;
use super::super::ship::Ship;
#[test]
fn assert_ship_placement_only_in_bounds() {
let mut ship = Ship::new(3);
let mut bf = Battlefield::new();
assert_eq!(Ok(()), bf.place_ship(&mut ship, 0, 0, Horizontal));
assert_eq!(Ok(()), bf.place_ship(&mut ship, 5, 5, Vertical));
}
}
src/battlefield.rs:166:47: 166:51 error: cannot borrow `ship` as mutable more than once at a time [E0499]
src/battlefield.rs:166 assert_eq!(Ok(()), bf.place_ship(&mut ship, 5, 5, Vertical));
^~~~
src/battlefield.rs:165:47: 165:51 note: first mutable borrow occurs here
src/battlefield.rs:165 assert_eq!(Ok(()), bf.place_ship(&mut ship, 0, 0, Horizontal));
^~~~
I know this is just a short excerpt, but the whole code is too much to post here. The project can be found here (standard build with 'cargo build').