4 ms·
I would be curious to know more about this case too, fwiw! Personally, I've only struggled a bit with the borrow checker in a couples of cases: trying to have
by spoiler 4y ago
I would be curious to know more about this case too, fwiw!
Personally, I've only struggled a bit with the borrow checker in a couples of cases: trying to have self/circular references when trying to model OOP, and when fiddling/interacting with some low level async code. Both were unnecessary in my case, and each situation was caused by a lack of understanding.
- Veliladon 4y agoYou 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.