I stumbled recently upon an artifact called unmovable struct: https://www.rust-lang.org/en-US/faq.html#how-can-i-define-a-struct-that-contains-a-reference-to-one-of-its-own-fields , which seems to be unique Rust anti-pattern There two things I wasn’t able to understand related to this:
I) Can I create an unmovable struct without
Cell-like wrapper? I suspect the answer is no, because
- I can’t move referenced object, therefore I can’t create reference before I create the unmovable struct
- To add reference later, I need a mutable (and therefore unique) reference to the filed to store reference (
yin the example)
- At the same time I need immutable reference to filed to be referenced, and that contradict thew point above
Is my reasoning correct, or am I missing something?
II) It would seem possible to create an unmovable struct on heap and then pass around pointer to it. However, the naive approach does not work: https://play.rust-lang.org/?gist=1e1579e2344fe60aedaaf3110bc7422b&version=stable . Are there any workarounds, or is it some fundamental restriction I didn’t think about?