<story><title>Rust data structures with circular references</title><url>https://eli.thegreenplace.net/2021/rust-data-structures-with-circular-references/</url></story><parent_chain></parent_chain><comment><author>bilkow</author><text>I think there is a slight mistake in the article:&lt;p&gt;&amp;gt; So how can a reference in a child assume the same lifetime? It can&amp;#x27;t.&lt;p&gt;Well actually, it can, because the owned Struct itself (Node) can outlive any borrow. You won&amp;#x27;t be able to mutate the parent though, as you can&amp;#x27;t mutate any value that has an active immutable borrow. That&amp;#x27;s the problem: you can&amp;#x27;t add the child node as adding it would mutate the parent and the there&amp;#x27;s an active immutable borrow (in the child node).&lt;p&gt;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&amp;#x27;re retrieving it. It also seems a lot better since there&amp;#x27;s less data in-tree to change if the structure itself changes.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;old.reddit.com&amp;#x2F;r&amp;#x2F;rust&amp;#x2F;comments&amp;#x2F;qstlto&amp;#x2F;rust_data_structures_with_circular_references&amp;#x2F;hkg1omp&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;old.reddit.com&amp;#x2F;r&amp;#x2F;rust&amp;#x2F;comments&amp;#x2F;qstlto&amp;#x2F;rust_data_stru...&lt;/a&gt;
[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;play.rust-lang.org&amp;#x2F;?version=stable&amp;amp;mode=debug&amp;amp;edition=2018&amp;amp;gist=731ab0fcad708964792f4b30e3aef2da&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;play.rust-lang.org&amp;#x2F;?version=stable&amp;amp;mode=debug&amp;amp;editio...&lt;/a&gt;</text></comment>
<story><title>Rust data structures with circular references</title><url>https://eli.thegreenplace.net/2021/rust-data-structures-with-circular-references/</url></story><parent_chain></parent_chain><comment><author>beltsazar</author><text>Similar to the second approach, you can have better ergonomics and performance by using a memory arena library like slotmap. A doubly linked list implemented using slotmap: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;orlp&amp;#x2F;slotmap&amp;#x2F;blob&amp;#x2F;ce6e1e02bb2c2074d8d581e87ad9c2f72ce495c3&amp;#x2F;examples&amp;#x2F;doubly_linked_list.rs&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;orlp&amp;#x2F;slotmap&amp;#x2F;blob&amp;#x2F;ce6e1e02bb2c2074d8d581e...&lt;/a&gt;</text></comment>