Yes, of course you should write
fn initialize() -> Vec<Vec<MyStruct>> { ... }
(BTW, Vec not that big - it's just three integers with a pointer, but still)
Rust has an RVO, and this is advertised in manuals like here . You yourself see it.
#[inline(never)] fn initialize() -> Vec<i32> { Vec::new() } fn main() { let v = initialize(); }
If you run this program in the playpen using the "asm" button, among everything else you can see this:
_ZN10initialize20h5d5903a85c1850a8eaaE: .cfi_startproc movq $1, (%rdi) movq $0, 16(%rdi) movq $0, 8(%rdi) movq %rdi, %rax retq
Vec::new() was built in, but nevertheless, you can see the idea: the address for a new Vec instance is passed to the function in %rdi , and the function stores the Vec fields directly in this memory, avoiding unnecessary copying through the stack. And here is what it's called:
leaq (%rsp), %rdi callq _ZN10initialize20h5d5903a85c1850a8eaaE
You can see that ultimately the Vec instance will be pushed directly on the stack.
source share