3 ms·
So why is the lifetime of the return value linked to the lifetime of the function argument? Couldn't the return value live longer than the function argument in
by mike-cardwell 9y ago
So why is the lifetime of the return value linked to the lifetime of the function argument? Couldn't the return value live longer than the function argument in that example?
- steveklabnik 9y agoIn both cases, the iterators are over the slice, so their lifetimes need to be tied together. The iterator can't live longer than the slice, or it'd be a use after free. Does that make sense?
- mike-cardwell 9y agoYes. Thanks for the explanation.
- irishsultan 9y agoThe Cloned iterator clones the elements of the underlying iterator, but only on-demand, so obviously it can't do that once the argument isn't live anymore.