4 ms·
I think you don't understand the Rust feature the post is talking about. It's not about returning a pointer to a struct. It's about the pointer being _safe_.
by hasenj 10y ago
I think you don't understand the Rust feature the post is talking about.
It's not about returning a pointer to a struct. It's about the pointer being _safe_.
What if x was not allocated on the stack, but on the heap? y is part of x (not another independent object) so if x is freed/deleted then all references to y become invalid. Does D have a guarantee that this won't happen? (I think the answer is no).
This was noted in the post itself:
> Most other languages simply prevent you from giving away
> an interior reference, or require y to refer to a distinct
> heap object from the X.
- Manishearth 10y agoThe D example is also safe. The difference is that the D example doesn't generalize (yet), whereas the Rust example does.