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

Daniel Micay danielmicay at gmail.com
Fri Jan 17 17:09:28 PST 2014


On Fri, Jan 17, 2014 at 8:07 PM, Daniel Micay <danielmicay at gmail.com> wrote:
> 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.

For example, you can have a type with private fields and the
`#[no_send]` attribute. You can then take it by-value in a function
returning the thread-safe type without `#[no_send]`. If it has a
destructor, then the caller is unable to implicitly copy it without
moving ownership and you would need to implement `Clone` to permit
making more instances.


More information about the Rust-dev mailing list