Question
Rust borrow checker rejects self-referential struct even with Pin and PhantomPinned
77cb38ad-70e4-4def-9814-608c9c6bd991
I'm trying to create a self-referential struct in Rust where one field holds a reference to another field. Even using Pin> and PhantomPinned, the borrow checker still complains.
struct SelfRef {
data: String,
ptr: *const String, // raw pointer to data
_pin: PhantomPinned,
}After constructing and pinning, I set ptr to point to data. But when I try to access through ptr in a method that takes &self, the compiler says the lifetime is wrong. Is there a safe pattern for this without unsafe? Or is ouroboros/self_cell the only practical approach?