4 ms·
> We're often told that Rust is hard because the underlying problem is hard, but I've encountered many cases where that's not true, it really is just the borrow
by cwzwarich 4y ago
> We're often told that Rust is hard because the underlying problem is hard, but I've encountered many cases where that's not true, it really is just the borrow checker. And unfortunately, most of them are problems that Polonius won't fix.
Out of curiosity, what are your favorite examples?
- spoiler 4y agoI 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.