The complete error message states:
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`
Which is useful to understand where the move is occurring.
The simplest solution is to implement either Copy or Clone for your AngleUnit type. If it's Copy, your code will work as-is. If it's only Clone, you have to explicitly call .clone() to make a duplicate.
If your type cannot be made Copy, then you can use references, as the compiler suggests:
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));
}
The original problem all boils down to this line:
let point_unit = self.unit;
What should the value of point_unit be?
If we moved the value from self.unit to point_unit, then what would the value of self.unit be? The "easy" solution would be that it is undefined memory, but experience has shown that we programmers will screw that up and introduce exciting-to-debug problems.
We could copy the value automatically, but what would happen if AngleUnit were a type that took up 10 MiB of space? Then an innocent looking line just sucked up a bunch of memory and time. That's not very nice either.
Instead, Rust makes it so that types are moved by default and you cannot leave an object in an undefined state. Certain types can opt into the ability to be automatically copied — this is the Copy trait. You can also allow types to be explicitly copied — the Clone trait. You can also obtain a reference to an existing value and pass that around. The borrow checker will prevent you from using that reference after it is no longer valid.