Design patterns for dropping associated data?
⚓ Rust 📅 2026-03-03 👤 surdeus 👁️ 1In Rust, we sometimes use ID-keyed lookup structures to store data in a way that avoids the ownership problems with circular data structures. I was thinking of using this pattern to store some extra info about Things in another struct, I’ll call Root. This Root could have a map from ThingID to ExtraInfo.
But the issue is I have been storing my Things as Rc<Thing>. If I add this extra info, the simplicity of Rc is defeated. The drop when the Rc count goes to zero will not clean up the map in Root.
Right now I’m thinking about some kind of garbage collection for the associated data. The ExtraInfo in the Root could also have an rc::Weak<Thing>, and periodically the entries for dead Things could be cleared out.
Any articles on this, or other ideas that would help?
2 posts - 2 participants
🏷️ Rust_feed