gpt4 book ai didi

rust - 将可变自引用传递给拥有对象的方法

转载 作者:行者123 更新时间:2023-11-29 08:33:33 25 4
gpt4 key购买 nike

以下是一个简单的模拟, field 是一个矩形区域,两个球在其中弹跳。 Field 结构有一个 update 方法,它在每个球上调用 update。在它们的 update 方法中,球需要根据它们的速度四处移动。但他们也需要相互 react ,以及领域的边界。:

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);
}
}

如何将关于边界和另一个球的信息提供给 Ball 结构的更新函数? Field::update 中的这些行不编译:

self.balls[0].update(self);
self.balls[1].update(self);

出现以下错误:

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

我明白了,但我不知道如何解决这个问题。

最佳答案

目前,您的 Ball 结构需要了解它所在的 Field 才能更新自身。这不会编译,因为结果将是循环引用与变异相结合。您可以使用 CellRefCell(后者具有性能成本)来完成这项工作,但以不同方式构建代码会更好。让 Field 结构检查并解析 Ball-BallBall-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
}

关于rust - 将可变自引用传递给拥有对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com