以下代码创建了一个玩家列表,然后将其他一些玩家的引用添加为朋友。我无法通过借用检查器。谁能解释我哪里出错?
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
}
要获得奖金,我如何让玩家进入游戏?
其余代码如下:
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(),
}
}
}
答案 0 :(得分:2)
我假设您已经搜索了错误消息,并通过<{3>}的部分进行了阅读,因此我将返回此优惠,不会浪费你的时间。相反,我将继续讨论借阅检查的问题:
error[E0502]: cannot borrow `players` as immutable because it is also borrowed as mutable
--> src/main.rs:40:25
|
39 | let mut alice = players.get_mut(0).unwrap();
| ------- mutable borrow occurs here
40 | alice.friends.push(&players[1]);
| ^^^^^^^ immutable borrow occurs here
...
48 | }
| - mutable borrow ends here
Rust只允许你拥有10+ questions and answers about it already。此代码已创建对向量的可变引用,并尝试获取第二个不可变引用。
必须禁止这样做,因为就编译器所知,更改可变引用可能会使不可变引用无效。这会导致记忆不安全,Rust不允许这样做。
在这种情况下,一旦您创建了players
向量,就不会再添加任何值。向量本身是不可变的,但其中的组件想要是可变的。这非常适合RefCell
:
struct Player<'a> {
name: String,
friends: RefCell<Vec<&'a Player<'a>>>,
}
现在Player
可以修改他们的朋友,而不知道包含可变性的包含元素。
然后,只需要删除向量的可变借用并将friends
作为可变对象借用:
let alice = &players[0];
alice.friends.borrow_mut().push(&players[1]);
alice.friends.borrow_mut().push(&players[2]);
您还必须向Game::new
添加缺少的生命周期,并编译:
use std::cell::RefCell;
struct Player<'a> {
name: String,
friends: RefCell<Vec<&'a Player<'a>>>,
}
impl<'a> Player<'a> {
fn new(name: &str) -> Player {
Player {
name: name.into(),
friends: RefCell::new(Vec::new()),
}
}
}
struct Game<'a> {
players: Vec<&'a Player<'a>>,
}
impl<'a> Game<'a> {
fn new(players: &[&'a Player<'a>]) -> Game<'a> {
Game { players: players.to_vec() }
}
}
fn main() {
let players = vec![
Player::new("alice"),
Player::new("bob"),
Player::new("eve"),
Player::new("dave"),
];
let alice = &players[0];
alice.friends.borrow_mut().push(&players[1]);
alice.friends.borrow_mut().push(&players[2]);
Game::new(&[&players[0], &players[1]]);
}
您的问题非常接近a single mutable reference or one-or-more immutable references to the same value,但略有不同。我们鼓励您阅读相关信息。