1
Fork 0

Clarify "owned data" in E0515.md

This clarifies the explanation of why this is not allowed and also what to do instead.

Fixes 62071

PS There was suggestion of adding a link to the book. I did not yet do that, but if desired that could be added.
This commit is contained in:
Marijn Schouten 2025-03-14 17:30:47 +01:00 committed by Marijn Schouten
parent f7b4354283
commit 50c659fcba

View file

@ -17,10 +17,13 @@ fn get_dangling_iterator<'a>() -> Iter<'a, i32> {
} }
``` ```
Local variables, function parameters and temporaries are all dropped before the Local variables, function parameters and temporaries are all dropped before
end of the function body. So a reference to them cannot be returned. the end of the function body. A returned reference (or struct containing a
reference) to such a dropped value would immediately be invalid. Therefore
it is not allowed to return such a reference.
Consider returning an owned value instead: Consider returning a value that takes ownership of local data instead of
referencing it:
``` ```
use std::vec::IntoIter; use std::vec::IntoIter;