相关疑难解决方法(0)

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

以下是一个简单的模拟,其中一个场是一个矩形区域,其中有两个球在其中弹跳.该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
        // …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

7
推荐指数
2
解决办法
1681
查看次数

标签 统计

borrow-checker ×1

rust ×1