Re:Rust 1.25 released
by mike-cardwell on Thursday March 29, 2018 @03:13PM
(#16710173)
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?
[ Parent
| on HN ]
Re:Rust 1.25 released
by steveklabnik on Thursday March 29, 2018 @03:51PM
(#16710481)
In 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?
Re:Rust 1.25 released
by mike-cardwell on Thursday March 29, 2018 @04:27PM
(#16710746)
Yes. Thanks for the explanation.
Re:Rust 1.25 released
by irishsultan on Thursday March 29, 2018 @04:30PM
(#16710771)
The 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.