Hacker News new | past | comments | ask | show | jobs | submit login

It does not guarantee, but it is actually quite hard to cause a memory leak by accident, because at the same time Rust makes creating cycles hard.



I actually haven't tried this... do you know off the top of your head what it takes to make a cycle with Rc?

It should not be possible with normal borrows (and safe Rust) since they're statically checked to be acyclic.


The Rust Book has an example: https://doc.rust-lang.org/book/ch15-06-reference-cycles.html

Of course, leaking memory is as easy as using `Box::leak`, although that's probably never going to happen accidentally.


You could store a `RefCell<Option<T>>` in an `Rc` and set it to `Some(rc)` after creation.

Or you could use the recently-stabilized `new_cyclic` method: https://doc.rust-lang.org/std/rc/struct.Rc.html#method.new_c...


And when using C libraries / FFI, there are a lot of tricky things going in, it is much easier to leak in unsafe land.


You're incorrect, sorry. It's very easy to creat cycles in inner-mutable data structures (e.g. Rc).




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: