I am stuck with either my objects are owned or my struct is owned.
I have the following code:
struct Person {
}
fn scan_persons<'a>() -> &'a [Person] {
let mut v: Vec<Person> = Vec::new();
let t = Person {}; <-- owned by function. what do I do?
v.push(t);
v.as_slice() <-- fails here
}
Slices (all borrows in general) are temporary views of data that has to have a more permanent storage somewhere else.
You can't borrow a temporary local variable and run away with a reference to it. The storage will be destroyed before the end of the function, so the view into that storage can't be returned beyond that function.