4 ms·
RefCells and Boxes just throw the same errors at runtime instead of compile time.
by j-krieger 6y ago
RefCells and Boxes just throw the same errors at runtime instead of compile time.
- steveklabnik 6y ago(RefCell yes, but box no)
- j-krieger 6y agoWoops, I guess I meant RC's and RefCells.
- estebank 6y agoRefCell is runtime "borrow checking" where a panic is thrown if the object is already mutably borrowed. Depending on your use case you might not even care about this behavior because you have other invariants that ensure that you're only borrowing mutably once, you just can't encode it in the type system. Otherwise, you would combine it with Rc[1]: Rc<RefCell<T>>. Box, on the other hand, doesn't have any runtime cost. In fact, it's runtime cost is the same as a borrow: both & and Box<T> are just plain pointers, with the extra benefit of having compile time lifetime checking. [1] https://doc.rust-lang.org/book/ch15-05-interior-mutability.html https://doc.rust-lang.org/book/ch15-05-interior-mutability.h...