3 ms·
One reason that comes to my mind is more control over resources. You can be sure the memory allocated for the two vectors in main is freed in add_first_element.
by idsout 9y ago
One reason that comes to my mind is more control over resources. You can be sure the memory allocated for the two vectors in main is freed in add_first_element.
- Symmetry 9y agoBut if ownership was returned then it could be freed in main. And because it was allocated on the stack in the first place there doesn't seem to be any reasonable way for the callee to, say, return a reference to an element of the vector because then the caller could return that reference again but then you'd have a value still referred to by the outside context sitting where the stack should be able to grow so I assume that Rust would disallow this and force you to make a copy of anything you want to return.