|
|
|
|
|
by marcus_cemes
297 days ago
|
|
To extend upon this, memory generally has a single owner. When it goes out of scope, it gets freed [1]. The drop() function, which appears analogous to free() in C/C++, is actually just an empty function who's sole purpose is to take ownership and make it go out of scope, which immediately frees the memory [2]. > This function is not magic; it is literally defined as: pub fn drop<T>(_x: T) {} This is usually more deterministic than GC languages (no random pauses), but can be less efficient for highly nested data structures. It also makes linked lists impossible without using "unsafe rust", as it doesn't abide by the normal ownership rules. [1]: https://doc.rust-lang.org/rust-by-example/scope/raii.html
[2]: https://doc.rust-lang.org/std/mem/fn.drop.html |
|
Admittedly not the easiest language to make a linked list in.