I know that A is the top level struct and Some will be valid through the life of A, and therefore putting references of Some inside _T_s should be ok. How do I do that, how do I avoid “does not live long enough” error? Is it event possible in safe Rust, or is it the “disallowed self referencing non-movable struct” thing?
Yeah, it’s that.
Safe Rust does not allow a struct to contain a reference to itself.
If you really need self-references:
- Wrap them in
- Use raw/unsafe pointer
*const Some. Be careful not to move the struct, e.g. use
Box::new(A)before getting the address.
Also consider another design:
- Store an integer index to a
Vecinstead of a real reference.
- Hold the Vec outside the struct (internal references to a borrowed slice are OK, since they all point outside the struct)