5 ms·
A friend of mine was actually asked this in an interview (how does substring works). The initial idea of taking an advantage of the immutability of an object wa
by eranation 12y ago
A friend of mine was actually asked this in an interview (how does substring works). The initial idea of taking an advantage of the immutability of an object was nice, but it turns out to be a big GC issue if your original string is extremely large.
- dfkf 12y agoIt is one of those cases where you can actually leak memory with a GC. But at least Java gives (gave?) you a choice: to create a new string a use the current one, in .NET substring always returns a new string.
- jeffdavis 12y agoThis is why I find rust intriguing. It tracks the lifetimes using the type system, so it would be obvious to the programmer that the large strings would need to live as long as the list holding the small substrings. I'm not a rust expert, but I believe that it's basically impossible to write something like the code in the bug report without getting a compile time error in rust. You'd either need to use a version of substring that makes a copy, or explicitly keep the large strings around.
- pcwalton 12y agoYeah, in Rust you'd have to go out of your way to use Rc to reference count the large strings.