I'm not sure how you intend to use this. When no variables are captured, the borrow checker will not have any issues with the closure method.
When you are still capturing, you could implement a macro like one answer suggests. However, IMO this highly depends on the complexity of the duplicated code and even then I don't immediately see what the benefits compared to extracting it as a closure/function are.
Then I think
RefCell
is exactly what you want to defer the mutable borrow to runtime instead of compile time: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3170c01849dc577a501ecb11dd44c5ba (source for this method on StackOverflow).Maybe there could be syntactic sugar to use captures implicitely as
RefCell
s inside a closure. But I would not be a fan of implicitely allowing multiple mutable borrows without any clue for that in the code.