Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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



Linked lists to arbitrary memory, yes. Linked list from a consecutive chunk of memory managed by a bump allocator: just as easy as any language, no need for unsafe.

Admittedly not the easiest language to make a linked list in.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: