patterns icon indicating copy to clipboard operation
patterns copied to clipboard

Circular references (bidirectional linked lists, etc)

Open vchekan opened this issue 4 years ago • 2 comments

Cyclic references in Rust are impossible in straightforward way and workarounds are required.

  1. Describe reason why it is so.
  2. Show different examples when cyclic reffereneces are present
  3. Describe solutions (Rc, Vec with storing indexes, Slotmap)

While problem with linked list is simple to grasp, sometimes when the proble manifest itself on architecture level, it is more difficult to realize that you are dealing with cyclic references. Perhaps this presentation can be an example for architecture that can benefit: https://www.youtube.com/watch?v=P9u8x13W7UE

Relevant reddit thread: https://www.reddit.com/r/rust/comments/kq6lt2/slotmap_10_has_been_released_copy_restriction/

vchekan avatar Jan 10 '21 19:01 vchekan

Should this be an idiom?

marcoieni avatar Jan 10 '21 20:01 marcoieni

I don't think it is an idiom, it is sort of a pattern. Usually when circular links are involved, it is better to have reference to another storage, but beginners don't know that (like when I get started). Other options includes using a bump allocator or even plain Vec. When creating games, one would most likely hit this.

pickfire avatar Jan 12 '21 16:01 pickfire