Ben*_*ley 7 rust borrow-checker
以下是一个简单的模拟,其中一个场是一个矩形区域,其中有两个球在其中弹跳.该Fieldstruct有一个update方法,它调用update每个球.在他们的update方法中,球需要根据它们的速度移动.但他们也需要相互反应,以及该领域的界限:
fn main() {
let mut field = Field::new(Vector2d { x: 100, y: 100 });
field.update();
}
#[derive(Copy, Clone)]
struct Vector2d {
x: i32,
y: i32,
}
struct Ball {
radius: i32,
position: Vector2d,
velocity: Vector2d,
}
impl Ball {
fn new(radius: i32, position: Vector2d, velocity: Vector2d) -> Ball {
Ball {
radius: radius,
position: position,
velocity: velocity,
}
}
fn update(&mut self, field: &Field) {
// check collisions with walls
// and other objects
}
}
struct Field {
size: Vector2d,
balls: [Ball; 2],
}
impl Field {
fn new(size: Vector2d) -> Field {
let position_1 = Vector2d {
x: size.x / 3,
y: size.y / 3,
};
let velocity_1 = Vector2d { x: 1, y: 1 };
let position_2 = Vector2d {
x: size.x * 2 / 3,
y: size.y * 2 / 3,
};
let velocity_2 = Vector2d { x: -1, y: -1 };
let ball_1 = Ball::new(1, position_1, velocity_1);
let ball_2 = Ball::new(1, position_2, velocity_2);
Field {
size: size,
balls: [ball_1, ball_2],
}
}
fn update(&mut self) {
// this does not compile
self.balls[0].update(self);
self.balls[1].update(self);
}
}
Run Code Online (Sandbox Code Playgroud)
如何获取有关边界和另一个球的信息到Ball结构的更新函数?这些行在Field::update不编译:
self.balls[0].update(self);
self.balls[1].update(self);
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
error[E0502]: cannot borrow `*self` as immutable because `self.balls[..]` is also borrowed as mutable
--> src/main.rs:62:30
|
62 | self.balls[0].update(self);
| ------------- ^^^^- mutable borrow ends here
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
Run Code Online (Sandbox Code Playgroud)
我明白了,但我不知道怎么解决这个问题.
这是一个较小的例子:
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: &Field) {}
}
struct Field {
ball: Ball,
}
impl Field {
fn update(&mut self) {
self.ball.update(self)
}
}
Run Code Online (Sandbox Code Playgroud)
当您传入对 的引用时Field,您就保证了 不能Field更改( “不可变引用”的不可变部分)。然而,这段代码也试图改变它的一部分:球!哪个参考应该是权威的,self或者field,在实施中Ball::update?
您可以将结构中需要的部分update和不需要的部分分开,并在调用函数之前update使用它们:
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: &u8) {}
}
struct Field {
players: u8,
ball: Ball,
}
impl Field {
fn update(&mut self) {
self.ball.update(&self.players)
}
}
Run Code Online (Sandbox Code Playgroud)
您甚至可以将这些零碎的参考资料捆绑到一个整洁的包中:
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: BallUpdateInfo) {}
}
struct BallUpdateInfo<'a> {
players: &'a u8,
}
struct Field {
players: u8,
ball: Ball,
}
impl Field {
fn update(&mut self) {
let info = BallUpdateInfo { players: &self.players };
self.ball.update(info)
}
}
Run Code Online (Sandbox Code Playgroud)
或者重构您的包含结构以将信息从头开始分离:
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: &UpdateInfo) {}
}
struct UpdateInfo {
players: u8,
}
struct Field {
update_info: UpdateInfo,
ball: Ball,
}
impl Field {
fn update(&mut self) {
self.ball.update(&self.update_info)
}
}
Run Code Online (Sandbox Code Playgroud)
self您也可以采取另一种方式,在对其进行任何更改之前将其Ball删除Field。如果您可以轻松/便宜地制作Ball,请尝试替换它:
use std::mem;
#[derive(Default)]
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: &Field) {}
}
struct Field {
ball: Ball,
}
impl Field {
fn update(&mut self) {
let mut ball = mem::replace(&mut self.ball, Ball::default());
ball.update(self);
self.ball = ball;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您无法轻松创建新值,可以使用Optionand takeit:
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: &Field) {}
}
struct Field {
ball: Option<Ball>,
}
impl Field {
fn update(&mut self) {
if let Some(mut ball) = self.ball.take() {
ball.update(self);
self.ball = Some(ball);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式将借用检查移至运行时而不是编译时RefCell:
use std::cell::RefCell;
struct Ball {
size: u8,
}
impl Ball {
fn update(&mut self, field: &Field) {}
}
struct Field {
ball: RefCell<Ball>,
}
impl Field {
fn update(&mut self) {
self.ball.borrow_mut().update(self)
}
}
Run Code Online (Sandbox Code Playgroud)
目前,您的Ball结构需要知道Field它所包含的内容才能更新自身.这不会编译,因为结果将是与突变结合的循环引用.您可以通过使用Cell或RefCell(后者具有性能成本)来完成此工作,但以不同方式构造代码会更好.让Fieldstruct检查并解决Ball- Ball和Ball- Wall碰撞.该Ball结构的update函数可以处理更新Ball的位置.
// Ball's update function
fn update(&mut self) {
// update position
}
// Field's update function
fn update(&mut self) {
for ball in self.balls.iter_mut() {
ball.update();
}
// check for collisions
// resolve any collisions
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1681 次 |
| 最近记录: |