Rollup merge of #138508 - hkBst:patch-3, r=wesleywiser

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:
Matthias Krüger 2025-03-17 22:49:05 +01:00 committed by GitHub
commit 01062ba233
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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
end of the function body. So a reference to them cannot be returned.
Local variables, function parameters and temporaries are all dropped before
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;