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

Why return an owned string instead of a borrowed str slice? A substring can always be borrowed and should never require a heap allocation?


Thanks for pointing that out, str slice is surely better. I returned String in all versions just because collect() returns A String and I want to reuse the same test function. Will update this later.


It's a trade-off. I've once seen a memory leak in a JSON-handling code which was caused precisely by JSON parser returning a view into the underlying raw input: we stored one or two string fields from a large-ish (5 MiB IIRC) JSON and it prevented that whole JSON blob from being garbage collected.


This probably won't be an issue in Rust because there are very few situations where taking a reference can extend the lifetime of an object. It can really only happen for temporary objects where their lifetime can be extend to that of the function. It isn't nearly the same as a GCed language where it is very easy to accidentally keep an object alive for a long time.




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

Search: