Rust has the idea of "implicit copyability", a property of a type formalized by the Pod trait. An implicitly copyable type is either a primitive, or a structure/enum which is built from implicitly copyable types (plus some extra rules in play here).
When you add a destructor (implementation of the Drop trait) to a type, it is no longer implicitly copyable. With the Drop implementation commented out, your value 'inner' is implicitly copyable, so when you create 'outer' it copies the contents (in this case there are none). When you have Drop, the creation of 'outer' *moves* the 'inner' value (because it is no longer implicitly copyable), hence the method call is no longer valid. Hope that helps! On Tue, Jan 21, 2014 at 2:18 PM, Igor Karablin <[email protected]> wrote: > Hello, > > I'm learning rust and trying to understand why rust allows me to compile > this piece of code. When Drop impl is uncommented - rustc complains about > 'use of moved value', which is ok i think. My question is - why it compiles > code when Drop is not implemented for Inner struct? If its behaves as > intended - where i can read about this difference? > > struct Inner; > impl Inner { > fn innerFn(&mut self) { > println!("Inner::innerFn"); // just for tracing > } > } > > struct Outer { > i: Inner > } > > // uncomment it, and we get error: use of moved value: `inner` > /* > impl Drop for Inner { > fn drop(&mut self) { > println!("Inner::drop"); // just for tracing > } > } > */ > > fn main() { > let mut inner = Inner; > let mut outer = Outer { i: inner }; // inner is moved into outer.i ? > inner.innerFn(); // why then i can call its method? > outer.i.innerFn(); > } > > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev > _______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
