Compile Time Guarantees
Static memory management at compile time:
- No uninitialized variables.
- No memory leaks (mostly, see notes).
- No double-frees.
- No use-after-free.
- No forgotten locked mutexes.
- No data races between threads.
- No iterator invalidation.
It is possible to produce memory leaks in (safe) Rust. Some examples are:
- You can for use
Box::leakto leak a pointer. A use of this could be to get runtime-initialized and runtime-sized static variables
- You can use
std::mem::forgetto make the compiler “forget” about a value (meaning the destructor is never run).
- You can also accidentally create a reference cycle with
- In fact, some will consider infinitely populating a collection a memory leak and Rust does not protect from those.
For the purpose of this course, “No memory leaks” should be understood as “Pretty much no accidental memory leaks”.