3 ms·
An extremely simple thing like having two objects stored in a struct where one object has a reference to the other is a Herculean task in Rust. This is not a la
by typon 5y ago
An extremely simple thing like having two objects stored in a struct where one object has a reference to the other is a Herculean task in Rust. This is not a language designed for prototyping...
- carlmr 5y agoYou can easily avoid such constructs in most cases though.
- typon 5y agoI've allocated both these things on the heap and Rust simply won't let me store them both together. I don't know about you but this is an extremely common pattern in almost all languages, you just don't think about it in the gc languages and in C++ storing pointers is no issue. The popularity of crates like rental also shows that it's not as easily avoidable as you suggest.
- jjnoakes 5y agoCan you post a simple code example? It is hard to imagine what the difficulty is.
- typon 5y agoYou can read through this SO question and the top response does a good job explaining the possible solutions: https://stackoverflow.com/questions/32300132/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct https://stackoverflow.com/questions/32300132/why-cant-i-stor...
- jjnoakes 5y agoSeems like using Box solves this issue fairly simply.
- hajile 5y agoThis is perhaps where I get hate from both sides, but I think such cases are best done in unsafe code blocks. A tool that makes something much harder without any meaningful gain should be avoided. Rust provides the tools to not have to fight the system and they should be used in these situations.
- typon 5y agoThat's what my solution is. I am not going to get into pins and get cargo crates to solve such a simple problem. I just resort to unsafe. But then at that point...C++ is easier for me.