Hacker News new | ask | show | jobs
by suremarc 1375 days ago
I don’t really understand what the borrow checker has to do with the automatic/manual memory management dichotomy.
1 comments

Even though ownership and borrowing are theoretically orthogonal, they're both needed to make a complete safe system (the context was about managing memory for safety, not merely having a syntax sugar for malloc/free.)

Unique ownership prevents leaks and double-free, and borrow checking prevents use-after-free. Borrowing is necessary to temporarily relax the exclusivity of uniquely-owned objects and access their interior. Access to interior of a unique_ptr (and shared_ptr) decays it to a reference or a bare pointer that is not managed by the smart pointer any more, and can be left dangling. Borrow checker eliminates the manual "be careful not to leave dangling pointers" part.