14 ms·
To 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 o
by dbaupp 9y ago
To 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