rustmoveborrow-checkerdereferenceborrow

When a type is `Copy` should a method move (`self`) or borrow (`&self`) the type?


In Rust, when a type is Copy, should a method move (self) or borrow (&self) the type?

When a method from a non-Copy type is called, there is a significant difference between a move (self) and a borrow (&self). For example, you would not be able to call a method that uses move (self) twice. However, for a Copy type, the difference between a move (self) and a borrow (&self) becomes less significant, at least for the caller.

In addition, as Rust will automatically dereference or borrow to look up a method, you can call a move (self) method or a reference (&self) method in the exact same manner.

I expect that there is little difference in each approach. When inlined by the Rust compiler, both appear to generate identical assembly code. At least from my experiment on the Godbolt compiler explorer. However, when not inlined the assembly code generated can be different. This is a trivial example, and therefore it should be taken with a grain of salt.

I couldn't establish a common practice from the standard library as there appears to be examples of both.

I don't think there should be much difference between each approach in the real world, although it does make me curious. Every time I implement Copy on a type, I wonder what the best approach is...

I did find an example, whereby I must implement From for both a value and a reference, otherwise the example will not compile. I am sure there are many more examples that exhibit similar behaviour, which may therefore make it more desirable to implment methods for either move (self) or borrow (&self).

What do you think is best? Does it matter? Is there a standard approach? Are there other cases that I should consider?

EDIT: At the moment I prefer to use a move (self) for Copy types.


Solution

  • My rule of thumb is:

    From is a different story; it makes sense to implement it for both owned and references.