On 2016-08-10, Melzzzzz <
m...@zzzzz.com> wrote:
> I don't understand? In Rust everything is moved by default. That is if
> compiler finds that value is moved it always complaints if there is
> other place where value is used.
> In such cases, one should make copy of value , which is in Rust
> implementing Clone trait and providing clone function.
> Of course, there is also Copy trait (derived from Clone) if calling
> clone is not convenient.
>
> One is not required to implement Clone as there is #[derive(Clone)]
> directive for automatic derivation, inspired by Haskell.
>
I think the problem you're having is that you assume that C++ 'move'
construction and assignment are analogous to Rust's notion of
ownership. Rust's ownership is a compile time property managing
lifetimes and keeping track of when data can be safely disposed of.
In C++, 'move' isn't really an operation. You have a special kind of
reference (an rvalue reference) and you can overload functions,
including the constructor and assignment operators, to behave
differently when they receive an rvalue reference.
The rvalue overload overloads of the constructor and assignment
operators are traditionally called 'move' constructors and 'move'
assignment operators. Both are allowed to do whatever you want, though
if you write some weird move constructor you will probably have great
sorrow and heaps of infelicity when you go to use your class with the
standard library or other code.
To be useful and integrate well with the rest of the language, the
move constructor MUST leave the 'moved-from' object in a valid
state. It doesn't have to be 'empty' it just has to be a valid
object. You will generally only bother writing a move constructor if
you can get some use out of it. So, say, if your class contains a
pointer to allocated memory, you might copy the pointer to the new
object and null it in the old (moved-from) object.
C++'s idea of movement makes no sense at all in Rust, since in C++ you
have to manage your own memory and manage lifetimes on your own and
all that other stuff. So, while they might not be what you're used to
or what would make sense in other languages, move semantics make sense
within the general shape of C++ as a language and as a set of
practices (like RAII).