4 ms·
I think there is a slight mistake in the article: > So how can a reference in a child assume the same lifetime? It can't. Well actually, it can, because the o
by bilkow 5y ago
I think there is a slight mistake in the article:
> So how can a reference in a child assume the same lifetime? It can't.
Well actually, it can, because the owned Struct itself (Node) can outlive any borrow. You won't be able to mutate the parent though, as you can't mutate any value that has an active immutable borrow. That's the problem: you can't add the child node as adding it would mutate the parent and the there's an active immutable borrow (in the child node).
This reddit comment[0] has one possible solution[1] to this problem: not saving all of those parent references in the node itself, only adding when you're retrieving it. It also seems a lot better since there's less data in-tree to change if the structure itself changes.
[0] https://old.reddit.com/r/rust/comments/qstlto/rust_data_structures_with_circular_references/hkg1omp/ https://old.reddit.com/r/rust/comments/qstlto/rust_data_stru...
[1] https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=731ab0fcad708964792f4b30e3aef2da https://play.rust-lang.org/?version=stable&mode=debug&editio...
- eliben 5y agoThanks, you're making a good point. I like how you phrased the problem here, will ponder about updating the wording of the post slightly to reflect that.