This is usually called NRVO (“Named Return Value Optimization”), because v in <Add for Array>::add could be using the return pointer.

I thought we had it, but I just looked at the relevant code and it’s not there.

There is this issue about it https://github.com/rust-lang/rfcs/issues/788 - and I vaguely recall explaining to the original poster how to implement simple NRVO in the compiler.

I’m not sure, there might be subtle issues around unwinding.

Half an hour later, I read the entire post again and I finally understood what the “move” in the title referred to.

However, I still don’t see what move operators would do to this example: the simplest possible move assignment in C++ that actually does something, is a Rust move.

The IR for the + operation, after cleaning it up a bit, looks like this:

call void <Array as Add>::add([20 x float]* c, Array* arg, Array* arg3)

RVO is already in effect on the caller side, but the callee doesn’t make use of it for v , and LLVM doesn’t optimize away the copy from v to c after inlining, not even on the latest nightly with -C opt-level=3 .