2 ms·
You can put recurring references in a type, you just need to Box it up first. So you'd have something like: enum List<T> { Cons(T, Box<List<T>>),
by Veliladon 4y ago
You can put recurring references in a type, you just need to Box it up first.
So you'd have something like:
enum List<T> {
Cons(T, Box<List<T>>),
Nil,
}
Since the list is now boxed up the borrow checker has a fixed sized object to put on the stack at compile time.
- spoiler 4y agoHi! Thanks for taking the time to explain the concept! I'm familiar with these (and others) now, but I was curious to know what others issues the commenter alludes to
- pornel 4y agoPrevious post mentioned self/circular references. You can't do that with a `Box`. You can with `Rc<RefCell<List` or `*mut List`, but those options either have a runtime cost, or aren't borrow-checked. Lack of self-referential types that are both safe and zero-cost is a legit limitation of Rust.