Who owns the unbound value?
Consider the following code:
struct MyStruct {
not_copyable: NotCopyable
}
struct NotCopyable;
fn main() {
let foo = MyStruct { not_copyable: NotCopyable };
foo.not_copyable;
foo.not_copyable;
// just found out the simpler "foo; foo;" will create the same problem
}
It won't compile with
src/main.rs:17:5: 17:21 error: use of moved value: `foo.not_copyable` [E0382]
src/main.rs:17 foo.not_copyable;
^~~~~~~~~~~~~~~~
src/main.rs:16:5: 16:21 note: `foo.not_copyable` moved here because it has type `NotCopyable`, which is non-copyable
src/main.rs:16 foo.not_copyable;
^~~~~~~~~~~~~~~~
error: aborting due to previous error
While I'm not very familiar with the ownership system yet, I think I understand why you weren't able to create two bindings let
to foo.not_copyable
. But in this case, there is no binding. So who owns not_copyable
here; where did he move?
source to share
So who owns `not_copyable here; where did he move?
No one. The expression foo.not_copyable
must deduce a value from the structure because that value is the result of the expression. That you are not doing anything with the value is near the point; you asked for a value, it gave you a value.
The compiler may be able to optimize this under certain circumstances, but without that it will move the value as you asked it.
And yes, it foo;foo;
will do the same: just as NotCopyable
it cannot be copied, it is not MyString
.
source to share