You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/2025/How to Avoid Fighting Rust Borrow Checker.md
+14-6Lines changed: 14 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -536,7 +536,7 @@ One benefit of interior pointer is to allow tight memory layout, without having
536
536
537
537
### Interior mutability summary
538
538
539
-
Mutable borrow exclusiveness is overly restrictive. It is not necessary for memory safety in single-threaded code. It's also . there is **interior mutability** that allows getting rid of that constraint.
539
+
Mutable borrow exclusiveness is overly restrictive. It is not necessary for memory safety in single-threaded code when not using interior pointer. There is **interior mutability** that allows getting rid of that constraint.
540
540
541
541
Interior mutability allows you to mutate something from an immutable reference to it. (Because of that, immutable borrow doesn't necessarily mean the pointed data is actually immutable. This can cause some confusion.)
542
542
@@ -582,7 +582,7 @@ In Rust, just having a mutable borrow `&mut T`, **Rust assumes that you can use
582
582
583
583
Another problem: It's hard to return a reference borrowed from `RefCell`.
584
584
585
-
As the previous example can be fixed by `Cell`, without `RefCell`, here is another contagious borrow exampleL
585
+
As the previous example can be fixed by `Cell`, without `RefCell`, here is another contagious borrow example:
586
586
587
587
```rust
588
588
usestd::collections::HashMap;
@@ -720,11 +720,11 @@ Similarily, `Arc` is the multi-threaded version of `Rc`. `Mutex` `RwLock` are th
720
720
721
721
[`QCell<T>`](https://docs.rs/qcell/latest/qcell/) has an internal ID. `QCellOwner` is also an ID. You can only use `QCell` via an `QCellOwner` that has matched ID.
722
722
723
-
The borrowing to `QCellOwner` "centralizes" the borrowing of many `QCell`s associated with it, ensureing mutable borrow exclusiveness. Using it require passing borrow of `QCellOwner` in argument everywhere it's used.
723
+
The borrowing to `QCellOwner` "centralizes" the borrowing of many `QCell`s associated with it, ensuring mutable borrow exclusiveness. Using it require passing borrow of `QCellOwner` in argument everywhere it's used.
724
724
725
-
QCell will fail to borrow if the owner ID doesn't mismatch. Different to `RefCell`, if owner ID matches, it won't panic just because nested borrow.
725
+
QCell will fail to borrow if the owner ID doesn't match. Different to `RefCell`, if owner ID matches, it won't panic just because nested borrow.
726
726
727
-
Its runtime cost is low: just check whether cell's id matches owner's id.
727
+
Its runtime cost is low. When borrowing, it just checks whether cell's id matches owner's id. It has memory cost of owner ID per cell.
728
728
729
729
One advantage of `QCell` is that the duplicated borrow will be compile-time error instead of runtime panic, which helps catch error earlier. If I change the previous `RefCell` panic example into `QCell`:
0 commit comments