error[E0515]: cannot return value referencing function parameter `d`
--> src/main.rs:9:9
|
9 | / Foo {
10 | | data: d,
11 | | data2: &d,
| | -- `d` is borrowed here
12 | | }
| |_________^ returns a value referencing data owned by the current function
I want to refer same variable as reference for a struct var, while having it borrowed as another.
Self-referential types are not possible in safe Rust. Split up your struct so that one type holds the data by value, while another one provides a borrowed view into it.
Thank you.
I think this should be allowed as the reference is immutable and ownership is with data var. For now I'll use the work around thank you everyone
The problem is that Rust requires all types to be relocatable. Moving an object to another place in memory is always allowed by the object's owner, as long as the owner hasn't lent out a reference. This is impossible If an object contains a pointer into itself: The pointer will continue referring to the old, invalid, location.
The more local problem with this code is that d resides in the new function's stack frame. That stack frame is destroyed when new() returns, and so the memory location that Foo::data2 is referring is no longer valid. You can get similar code to work by referring to the T on the calling function's stack, instead:
Here, though, changing the contents of Foo::data won't be reflected in Foo::data2.
Regardless of the definition of Foo, this code is incorrect. &mut references are non-aliasing: no other accesses to the referent are allowed while one exists. Here, the line let x = &mut foo.data claims exclusive access to foo.data until println!("{}", x), the last mention of x. The print of foo.data occurs between these two statements, which is forbidden.