Я пытаюсь написать игру Ti c -Ta c -Toe на консоли. Для одного метода моей Board
структуры я бы хотел вернуть все позиции определенного типа.
type PlayerID = i32;
type Position = (usize, usize);
#[derive(PartialEq)]
pub enum TileState {
Player(PlayerID),
None,
}
pub struct Board {
board: [[TileState; 3]; 3],
}
impl Board {
const ALL_POSITIONS: [Position; 3] = [(0, 0), (0, 1), (0, 2)];
pub fn free_tiles<'a>(&'a self) -> impl Iterator<Item = Position> + 'a {
Board::ALL_POSITIONS
.iter()
.filter(|(x, y)| self.board[*x][*y] == TileState::None)
.cloned()
}
}
Ошибка компилятора:
error[E0373]: closure may outlive the current function, but it borrows `self`, which is owned by the current function
--> src/lib.rs:20:21
|
20 | .filter(|(x, y)| self.board[*x][*y] == TileState::None)
| ^^^^^^^^ ---- `self` is borrowed here
| |
| may outlive borrowed value `self`
|
note: closure is returned here
--> src/lib.rs:18:9
|
18 | / Board::ALL_POSITIONS
19 | | .iter()
20 | | .filter(|(x, y)| self.board[*x][*y] == TileState::None)
21 | | .cloned()
| |_____________________^
help: to force the closure to take ownership of `self` (and any other referenced variables), use the `move` keyword
|
20 | .filter(move |(x, y)| self.board[*x][*y] == TileState::None)
| ^^^^^^^^^^^^^
Желательно, чтобы я хотел чтобы избежать необходимости копировать ALL_POSITIONS
, self.board
или создавать новый массив любым другим способом. Какой стиль Rust будет лучшим для решения этой проблемы?