3 ms·
I understand not copying mutable pointers; I don't understand preventing copies of pointers to immutable data. I also don't see why affine types allow rust to g
by weberc2 9y ago
I understand not copying mutable pointers; I don't understand preventing copies of pointers to immutable data. I also don't see why affine types allow rust to get away without a GC in a way that scoped-based memory doesn't.
- steveklabnik 9y ago&T is Copy, so I'm not sure what you mean with the first bit. For the second one, consider Box<T> vs uniq_ptr<T>; Rust can statically prevent use after-move, C++ cannot, and you'll get a NPE. That is, you can totally get rid of a GC through only RAII, but you can't guarantee memory safety (as far as we know!) without affine/linear types.
- dbaupp 9y agoTo be clear, Rust does scope-based memory management and does allow copying pointers to immutable data. Affine typing (plus the rest of Rust's system) on top of scoped memory management is needed to avoid problems like returning a pointers into something that is deallocated at the end of a function: int &foo() { std::vector<int> v = ...; return v[0]; } // return value is dangling And similarly, avoiding having pointers into things that are destroyed when their parent is modified: std::vector<std::unique_ptr<int>> v = ...; int &ref = *v[0]; v.clear(); // ref is dangling