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

标签 rust borrow-checker

以下是一个简单的模拟, 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/30681468/

相关文章:

rust - 是否可以在 &mut 周围创建一个包装器,其作用类似于 &mut

data-structures - 从可变结构字段的值调用可变方法

rust - 为什么 Rust 编译器在使用 Rust 1.31 时可以打破借用规则?

rust - 通过设置变量类型和使用 `as` 进行类型转换有什么区别?

file - 无法使用 std::fs 方法读取 Docker 镜像中的文件

generics - 为什么 rust 只允许数组大小的独立常量?

rust - 临时u8切片的Rust字符串

error-handling - 宏匹配臂模式 “no rules expected the token ` if`”

rust - 不能作为不可变借用,因为它也作为可变借用

rust - 使用方法调用结果时的多个可变借用