[rust-dev] wrapping a C library (ownership/mutability questions)

Daniel Micay danielmicay at gmail.com
Fri Jan 17 17:07:33 PST 2014


On Fri, Jan 17, 2014 at 7:53 PM, Josh Haberman <jhaberman at gmail.com> wrote:
>
> Are you saying (if I may lapse into C++ vocab for a moment) that I can't
> hide the copy constructor? Anyone can copy my non-mut struct into a
> mut struct at any time and I don't have any say in the matter?
>
> Feel free to correct this into Rust-speak.  :)

Every type in Rust can be assigned, passed or returned by-value. This
is always semantically equivalent to a shallow copy, as they are in C.
If the type has a destructor, closure or `&mut T` inside then the copy
is considered a move of ownership.

That's why there's a `Clone` trait in the standard library. It's the
minimum work to go from a non-owning reference to a value, rather than
just being the built-in shallow copy potentially moving ownership from
the source to the destination.

You're also free to make fields private.


More information about the Rust-dev mailing list