Problems with field ref in struct

In this case you can store dd along with a Vec<usize> containing indexes into kk rather than references.

That or look into some self-referential (that's the name of what you likely want) crate, like ouroboros.

For future reference, whenever you're trying to write a function that requires something with a lifetime (e.g. AA<'t> here) but there's nothing with a lifetime in input to the function then you're 99% trying to do something impossible, and you should stop and reason about who owns what. In your example here dd is a Vec owned by the function which will be destroyed when the function ends, so there's no way AA will be able to borrow from it.

11 Likes