Lifetimes in RefCells

Hello all,

I have an issue with lifetimes that I cannot manage to solve on my own. Anyone could explain me what the problem is and how to solve it ?

I want the variable p to keep ownership of the refcells and the map variable to be a way to index them differently. But I get this error on lifetimes that I don't understand nor know how to solve.

You cannot have references from one field of a struct to another.

Just change line 17 to fn foo(self: &'a mut Self) { and it compiles

Okay, fair enough, you can't have references from one field to another without irrevocably borrowing it for the rest of its existence, making it impossible to use the type after the call, except for ways compatible with that eternal borrow.


True. The simple example here can be made to compile but once you try doing anything else with this code, you're likely to get deadlocked by the borrow checker

Indeed I tried this fn foo(self: &'a mut Self) but couldn't use my object anymore afterwards in its methods... Is there a rust-ic way or pattern to do this without getting annoyed by the borrow-checker ?

Yes, don't try to make self-referential structs. E.g. you can put the objects in a vector and use indexes, or you can use Rc to share it.


for those looking for solution, I came up with this:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.