Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • It is more complicated way to write let mut parent = Parent::new(); let child = Child::new(&mut parent);, isn't it? Commented Feb 4 at 14:38
  • @magras No; in my case, the code to construct parent+child was complex, such that I needed a function to encapsulate that construction, to avoid repeating it everywhere (macros had worse drawbacks here). But encapsulating the construction of two objects (where child depends on parent) isn't easy, due to a ref between parent and child. So, I use a generic "data holder" that the caller creates first (always a simple one-liner), that the construction func merely fills (and for convenience, returns a temp ref to the child); caller owns that external "anchor" object, so func avoids dangling refs. Commented Feb 5 at 6:22