Rc, but Foo needs context to drop/cleanup?

⚓ Rust    📅 2026-06-30    👤 surdeus    👁️ 2      

surdeus

I have a tree of widgets that I'm currently managing with Rc. I wanted to experiment with SlotMap and put some things connected to these objects in a central owner/context (struct Window), but then I wonder how to cleanup. With plain Rc, when the last ref is dropped, the memory gets cleaned up. But what if my Window has a field like:

struct Window {
    ...
    things: SlotMap<WidgetId, Stuff>

}

I'd need to somehow get an &mut Window in the drop method for my widgets, so I could remove the entry in the SlotMap.

Is there a pattern for handling this?

8 posts - 4 participants

Read full topic

🏷️ Rust_feed