31 ms·
Rust doesn't have user-defined copy constructors and a return is always a move (semantically a memcpy), so I'm not sure it's so relevant as an optimisation for
by dbaupp 10y ago
Rust doesn't have user-defined copy constructors and a return is always a move (semantically a memcpy), so I'm not sure it's so relevant as an optimisation for Rust.
- santaclaus 10y agoNRVO is an optimization above a move, at least in C++. Most compilers simply toss a pointer to the returned to 'object' on the call stack for direct manipulation (correct me if I'm mistaken compiler peeps!), avoiding the extra potential internal surgery involved in a move operation.
- echelon 10y agoYou can't manually implement the `Copy` trait?
- Manishearth 10y agoThat makes a type copyable, but does not allow you to insert arbitrary code that must be executed during a copy.
- hornetblack 10y agoKinda but: trait Copy: Clone {} Copy is nearly always done by memcpy/memmove. When Copy is invoked it won't use the `Clone::clone` method: https://is.gd/2hMyNP https://is.gd/2hMyNP. i.e. Clone is always explicit. Added: Essentially Copy is the same as a move, but the original value is still valid.
- masklinn 10y agoYou can but it has no user-defined methods related to it, it's just a marker trait. Move and copy are both straight memcpy, the difference is whether the source remains valid afterwards, not what operation is performed.