Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Rust borrow checker is designed to enforce "one owner" model (a tree). When you need to have more than one reference, you can use Rc + Weak[0]. Example DoubleLinkedList implementation:

  struct Node<T> {
    pub data: T,
    pub prev: Option<Weak<RefCell<Node<T>>>>,
    pub next: Option<Rc<RefCell<Node<T>>>>,
  }
Moreover, if you have cycles instead of trees, you can use a garbage collector with support for cycles, like rust-cc[1].

So yes, it's cannot be done statically, because Rust is not designed for that.

However, problem disappears when 'static lifetime is used (or arenas). Nodes can be marked as deleted, instead of dropping them, so pointers are always valid.

In same vein, when nodes are deleted rarely, they can be simply marked as deleted, without dropping them completely (until sibling nodes are updated, at least):

  struct Node<T> {
    pub data: Option<T>,
    pub prev: Option<Weak<RefCell<Node<T>>>>,
    pub next: Option<Rc<RefCell<Node<T>>>>,
  }
When node is deleted (its payload is dropped), linked list is still walkable.

[0]: https://doc.rust-lang.org/std/rc/struct.Weak.html

[1]: https://github.com/frengor/rust-cc



Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: