1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-30 04:19:26 +02:00
Files
comprehensive-rust/src/why-rust/compile-time.md
Martin Geisler cf505e99cd Rephrase “No memory leaks”
The footnote was added before we had support for speaker notes. It’s
too large to show on the screen during a classroom presentation, so it
has now been moved to the speaker notes.

I tried to keep the information intact, including keeping the word
“mostly” on the slide.
2023-01-09 14:26:47 +01:00

1.1 KiB

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 NULL pointers.
  • 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::leak to leak a pointer. A use of this could be to get runtime-initialized and runtime-sized static variables
  • You can use std::mem::forget to make the compiler "forget" about a value (meaning the destructor is never run).
  • You can also accidentally create a reference cycle with Rc or Arc.

For the purpose of this course, "No memory leaks" should be understood as "Pretty much no accidental memory leaks".