Is it possible to force a move for a Copy trait type?

let s = S { ... }; // suppose S implements Copy
let s1 = s; // This is a copy. Can we force it to be a move with some special syntax?

Just curious to ask. I cannot think of any valid use cases as copy and move are both plane bit copies in this case. Besides, it doesn't seem to make sense to invalid s because it is, well, valid by definition. In addition there are no side effect differences anyway since S does not drop. But, maybe in certain cases such syntactical requirement is good to have (to denote some "ownership" transfer symbolically)?

Another way to ask: is there a C++ std::move equivalent in Rust? Or it's not needed in Rust in any possible cases due to the language design?

In a limited scope; you could just take a unique reference to it:

let s = S { ... };
let s1 = &mut s; //Ignore s from here on out.

There is no difference between a copy and a move besides the fact that you can access the previous value for Copy types. So there is no need for this sort of construct.

Note that with C++ std::move is necessary because copying things around is the default and copying can be expensive. std::move is a cheaper alternative to the default. With Rust the default is a bit-copy, doesn't get better than that, so there is no need for something like std::move


If for some reason you want to enforce moving a type that is already Copy, you can wrap it in a newtype struct on which you don't impl Copy.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.