Hacker News new | ask | show | jobs
by nabeelms 1027 days ago
About String::leak:

> The caller has free choice over the returned lifetime, including 'static. Indeed, this function is ideally used for data that lives for the remainder of the program’s life, as dropping the returned reference will cause a memory leak.

I can see the use case of the function and from reading the commit discussion, it seems it was based on Vec::<u8>::leak() but I'm not able to understand why either of the functions are safe instead of unsafe.

2 comments

Rust's memory safety model does not prohibit memory leak.

https://doc.rust-lang.org/book/ch15-06-reference-cycles.html

Bjarne Stroustrup is emphatic that prohibiting all leaks must be part of safety, but I have no idea what the logical justification is to distinguish say Box::leak, which this prohibits, from the moral equivalent of freeing resources only on exit, which is fine under such a rule.

It composes (if A doesn't leak and B doesn't leak, then a combination AB also does not leak) which is a property safety also has, but I don't see another connection.

> I'm not able to understand why either of the functions are safe instead of unsafe.

https://cglab.ca/%7Eabeinges/blah/everyone-poops/