0

Why can't we use RefCell for recursive data structures in Rust?

Invalid:

enum List {
    Cons(i32, RefCell<List>),
    Nil,
}

Valid:

enum List {
    Cons(i32, Rc<List>), // or Box<List>
    Nil,
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
sidoshi
  • 2,040
  • 2
  • 15
  • 30
  • Is this similar enough to [Why are recursive struct types illegal in Rust?](https://stackoverflow.com/questions/25296195/why-are-recursive-struct-types-illegal-in-rust) that it can be closed as a duplicate? – trent Jan 03 '19 at 17:12
  • This question is different. I understand why recursive types are invalid and that we can solve that using indirection. But I assume all smart pointers provide indirection since, after all, they are pointers. So my question is is why doesn't RefCell provide indirection? – sidoshi Jan 04 '19 at 04:27

1 Answers1

4

RefCell contains the object inside, wrapping it, it's not a heap allocated value. That's why the compiler says "recursive without indirection": RefCell is not an indirection.

Box and Rc, on the other hand, contain a reference to an object allocated somewhere else, and therefore are indirections.

Without an indirection, the List enum ends up being infinite since every Cons contains a full List enum inside its RefCell.

You can not do recursive data structures that contain themselves as values, only as pointers or references.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105