How should I store a dyn FnMut in a struct?
18:37 18 Dec 2025

I'm trying to make a struct that has dynamic methods by storing FnMuts in the struct. The FnMuts would be a function written in a scripting language like Python or Lua, but there might be some cases where it is a native Rust function; thus, they need to be dyn FnMuts. Because they're dyn, the struct needs to hold references to the FnMuts, and they need to be mutable references since they are FnMuts. Additionally, if an instance of the struct has the same functions as another, I want them to reference the same FnMuts instead of referencing separate duplicates. I can think of some ways to do this, like RefCell or Pin, but I would like to know what the most ideal and idiomatic way to do this is, because none of the solutions I can come up with seem "right". If I was using Fns, I could just use Rc or Arc and be satisfied, but the functions need to be able to mutate their corresponding object.

rust reference smart-pointers