3 ms·
The article is not about returning a reference to a struct member, its about returning a reference to something on the stack rather than heap.
by willvarfar 10y ago
The article is not about returning a reference to a struct member, its about returning a reference to something on the stack rather than heap.
- imron 10y agoAnd crucially, enforcing that no references to the returned member will outlive the containing struct.
- topspin 10y agoRust ownership applies to heap allocated objects as well; if you return a reference to a member of a heap allocated struct that reference cannot outlive the struct allocation. The point of the article is more general than "stack vs heap;" other languages are either incapable of expressing a reference to a member of a heap allocated struct or incapable of ensuring the memory safety of such a reference.
- willvarfar 10y agoAs the article notes, garbage-collected languages can ensure the memory safety of a pointer into a heap object.
- TuringTest 10y agoBut not at compile time, which is what Rust provides.