Hacker News new | ask | show | jobs
by LecroJS 1315 days ago
I have only dabbled in Rust and have no experience with C++/Java/Go. Can you help me understand how those languages would be more memory safe than Rust? My understanding was that safe code was a solution to memory leaks. What’s the benefit to safe rust code if it isn’t getting rid of memory leaks?
3 comments

Because memory leaks are memory safe in rust, they just cause a performance hit.
Memory leaks are not memory safety.

Memory leaks in all languages are safe, they just slow performance and cause crashes.

Rust still memory leaks.

Thanks. So what does it mean for a program to be memory safe if it can still memory leak? An example or pointing to a resource for further reading would be much appreciated.
It prevents data races, use-after-free, double-free, buffer overflow, invalid type punning, etc. You can still do all those things in unsafe code though, and you could in safe code if the unsafe code you depend on (including the kernel) behave/are programmed incorrectly. You can also have hardware issues and stochastic bit flips that Rust and SPARK can't deal with.
> So what does it mean for a program to be memory safe if it can still memory leak?

That it's not actually memory safe. Memory leaks are part of memory unsafety.

That’s not correct. Memory safety consists of properties necessary for type safety to be upheld. Leaks alone can crash a program but can’t defeat type safety.
Memory safe code does not prevent applications from using too much memory; it prevents applications from accessing the wrong memory (e.g., by using a variable after its underlying value has been freed and the memory reallocated).