我正在学习 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`
这有助于了解移动发生的位置。
最简单的解决方案是实现Copy
或 Clone
为你的 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/