Why do all docs say RefCell is bad?

Sometimes people find themselves using Rc<RefCell<T>> because they're trying to write code the way they would in a garbage-collected language, and the borrow checker is getting in the way. In a sense RefCell is a bludgeon you can use to make your design work, but it's often worse than starting from a better design and not needing it at all.

To learn more, I suggest watching Catherine West's RustConf 2018 closing keynote on using Rust in game development. Or read the blog version if you prefer. ECS isn't the be-all end-all of data-oriented design, but it's a great example of how putting a bit more thought into your data can make problems (like using RefCell everywhere) almost evaporate.

I notice that your example doesn't require interior mutability at all; Box<Node> works just as well, without changing any method signatures. (And in fact, Node alone would also work, although it's not a pointer.)

9 Likes