rust - Who owns a value without a let binding? -
consider following code:
struct mystruct { not_copyable: notcopyable } struct notcopyable; fn main() { let foo = mystruct { not_copyable: notcopyable }; foo.not_copyable; foo.not_copyable; // found out simpler "foo; foo;" create same problem }
this fails 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 has type `notcopyable`, non-copyable src/main.rs:16 foo.not_copyable; ^~~~~~~~~~~~~~~~ error: aborting due previous error
while i'm still not versed in ownership system, think why couldn't create 2 let
bindings foo.not_copyable
. in case there no binding. owns not_copyable
here; did move?
so owns `not_copyable here; did move?
no one. expression foo.not_copyable
has pull value out of structure, because value result of expression. don't do value beside point; asked value, gave value.
it's possible compiler might able optimise out under circumstances, without that, it's going move value asked to.
and yes, foo;foo;
same thing: notcopyable
not copyable, neither mystring
.
Comments
Post a Comment