Cannot borrow mutably from a RefCell<FnMut>


#1

I am having an issue passing around an FnMut, I do not understand exactly why this does not compile

   fn respond(order: Order, func: Rc<RefCell<FnMut(Order)->()>>) -> () {
                let mut caller = func.borrow_mut();
                caller(order);
    }

error[E0596]: cannot borrow immutable borrowed content as mutable
  --> src/main.rs:15:17
   |
15 |                 caller(order);
   |                 ^^^^^^ cannot borrow as mutable

playground link: playground


#2

Found the issue detailed here github

I solved it by calling it like:

(&mut *func.borrow_mut())(order);

It looks horrible though


#3

You need to get the &mut FnMut out of the RefMut that borrow_mut returns:

let caller = &mut *func.borrow_mut();

#4

This was cross-posted to Stack Overflow