[rust-dev] should fail to compile or not?

Igor Karablin karablin at gmail.com
Tue Jan 21 17:14:42 PST 2014


Yes, this is really cleared all mystery, thanks for your explanation Alex!


On Wed, Jan 22, 2014 at 5:02 AM, Alex Crichton <alex at crichton.co> wrote:

> 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 <karablin at gmail.com> 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
> > Rust-dev at mozilla.org
> > https://mail.mozilla.org/listinfo/rust-dev
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140122/fac24e3d/attachment.html>


More information about the Rust-dev mailing list