1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-14 21:35:00 +02:00

Merge pull request #28 from zdimension/patch-2

Add footnote about memory leaks
This commit is contained in:
Martin Geisler
2022-12-22 20:29:10 +01:00
committed by GitHub

View File

@ -3,10 +3,12 @@
Static memory management at compile time: Static memory management at compile time:
* No uninitialized variables. * No uninitialized variables.
* No memory leaks. * *Mostly* no memory leaks[^leaks].
* No double-frees. * No double-frees.
* No use-after-free. * No use-after-free.
* No `NULL` pointers. * No `NULL` pointers.
* No forgotten locked mutexes. * No forgotten locked mutexes.
* No data races between threads. * No data races between threads.
* No iterator invalidation. * No iterator invalidation.
[^leaks]: It is technically possible to produce a memory leak in (safe) Rust. The [`Box::leak`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak) method allows getting a raw reference out of a [`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html) and dropping the [`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html) afterwards, without running the destructor. A use of this could be to get runtime-initialized and runtime-sized static variables. Or simply, the [`std::mem::forget`](https://doc.rust-lang.org/std/mem/fn.forget.html) function, which makes the compiler "forget" about a value meaning the destructor is never run. There are many other ways to create leaks in safe Rust, but for the purpose of this course, "No memory leaks" should be understood as "Pretty much no *accidental* memory leaks".