rust - 从结构字段分配变量时为 "cannot move out borrowed content"

标签 rust borrow-checker borrowing

我正在学习 Rust,我正在与借用检查器作斗争。

我有一个基本的 Point 结构。我有一个 scale 函数可以修改点的所有坐标。我想从另一个名为 convert 的方法调用此方法:

struct AngleUnit;

struct Point {
    x: f32,
    y: f32,
    z: f32,
    unit: AngleUnit,
}

fn factor(_from: AngleUnit, _to: AngleUnit) -> f32 {
    1.0
}

impl Point {
    pub fn new(x: f32, y: f32, z: f32, unit: AngleUnit) -> Point {
        Point { x, y, z, unit }
    }

    fn scale(&mut self, factor: f32) {
        self.x *= factor;
        self.y *= factor;
        self.z *= factor;
    }

    fn convert(&mut self, unit: AngleUnit) {
        let point_unit = self.unit;
        self.scale(factor(point_unit, unit));
    }
}

现在我有以下错误:

cannot move out of borrowed content

我做错了什么?

最佳答案

完整 错误消息指出:

error[E0507]: cannot move out of borrowed content
  --> src/lib.rs:26:26
   |
26 |         let point_unit = self.unit;
   |                          ^^^^^^^^^
   |                          |
   |                          cannot move out of borrowed content
   |                          help: consider borrowing here: `&self.unit`

这有助于了解移动发生的位置。

最简单的解决方案是实现CopyClone为你的 AngleUnit类型。如果是 Copy ,您的代码将按原样工作。如果它只是 Clone , 你必须显式调用 .clone()制作副本。

如果你的类型无法制作Copy ,那么您可以使用引用,正如编译器建议的那样:

fn factor(_from: &AngleUnit, _to: &AngleUnit) -> f32 {
    1.0
}
fn convert(&mut self, unit: AngleUnit) {
    let point_unit = &self.unit;
    self.scale(factor(point_unit, &unit));
}

原来的问题都归结为这一行:

let point_unit = self.unit;

point_unit的值应该是多少?是吗?

如果我们移动 self.unit 中的值至 point_unit ,那么 self.unit 的值是多少?是? “简单”的解决方案是它是未定义的内存,但经验表明我们程序员会搞砸它并引入令人兴奋的调试问题。

我们可以自动复制这个值,但是如果 AngleUnit 会发生什么?是一种占用 10 MiB 空间的类型吗?然后,一条看似无辜的台词吸走了一堆内存和时间。这也不是很好。

相反,Rust 使类型默认移动,并且您不能让对象处于未定义状态。某些类型可以选择自动复制的能力——这是 Copy特征。您还可以允许显式复制类型 — Clone特征。您还可以获得对现有值的引用 并将其传递。借用检查器将在该引用不再有效后阻止您使用该引用。

关于rust - 从结构字段分配变量时为 "cannot move out borrowed content",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34621969/

相关文章:

rust - 如何限制关联类型接受任何引用生命周期?

multithreading - 在 amd64 上拆分堆栈是不必要的

rust - 核心中的运算符真的是循环定义的吗?

rust - 如何从Rust字符串创建* mut u8?

rust - 值(value)活得不够长

rust - 在 Rust 中使用引用和使用拥有的值有区别吗?

rust - Rust 的借用规则是否妨碍了功能数据结构?

rust - include_bytes!() 可以用于自定义结构吗?

rust - 如何实现支持可变迭代器的容器?

collections - 在迭代器上调用map时如何消除部分移动