我想将一个 FnMut
闭包封装在一个 RefCell
中,如下所示:
fn borrow_mut_closure() {
let mut temp = 3i32;
let cl = RefCell::new(move || {
temp += 1;
println!("{}", temp);
});
cl.borrow_mut()();
}
但令我惊讶的是,编译器报告:
cannot borrow data in a dereference of `std::cell::RefMut<'_, [closure@src/main.rs:17:25: 20:4 temp:i32]>` as mutable
cannot borrow as mutable
help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::cell::RefMut<'_, [closure@src/main.rs:17:25: 20:4 temp:i32]>`rustc(E0596)
但为什么没有实现呢?我该怎样克服这个问题呢?