The following code creates a list of players then adds references to some of the other players as friends. I can't get this past the borrow-checker. Could anyone explain where I'm going wrong?
fn main() {
let mut players =
vec![Player::new("alice"), Player::new("bob"), Player::new("eve"), Player::new("dave")];
let mut alice = players.get_mut(0).unwrap();
alice.friends.push(&players[1]);
alice.friends.push(&players[2]);
// The list is borrowed by the existence of Alice, how do I
// put Bob and Eve into Alice's group?
let game = Game::new(GameType::Checkers,
"Game #23",
vec![&players[0], &players[1]][..]);
// the trait bound `[&'a Player<'_>]: std::marker::Sized` is not satisfied
}
For a bonus, how do I equally get players into the game?
The rest of the code is as follows:
struct Player<'a> {
name: String,
friends: Vec<&'a Player<'a>>,
}
impl<'a> Player<'a> {
fn new(name: &str) -> Player {
Player {
name: name.into(),
friends: Vec::new(),
}
}
}
enum GameType {
Chess,
Checkers,
SnakesAndLadders,
}
struct Game<'a> {
game: GameType,
name: String,
players: Vec<&'a Player<'a>>,
}
impl<'a> Game<'a> {
fn new(game: GameType, name: &str, players: [&'a Player]) -> Game<'a> {
Game {
game: game,
name: name.into(),
players: players.to_vec(),
}
}
}