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:
parent
f7b4354283
commit
50c659fcba
1 changed files with 6 additions and 3 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue