Re:Mrustc: a Rust compiler written in C++
by dbaupp on Monday September 25, 2017 @08:58PM
(#15335378)
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
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:
And similarly, avoiding having pointers into things that are destroyed when their parent is modified: