som*_*guy 3 closures loops rust
我有一个struct类似的东西:
struct Foo<'a> {
callbacks: Vec<&'a FnMut(u32)>,
}
Run Code Online (Sandbox Code Playgroud)
我想调用每个回调,但是我的尝试不起作用:
fn foo(&mut self) {
for f in &mut self.callbacks {
(*f)(0);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error: cannot borrow immutable borrowed content `**f` as mutable
Run Code Online (Sandbox Code Playgroud)
我也尝试过,iter_mut()但是得到了同样的错误。
FnMut 需要一个可变的接收器,因此您必须具有可变的引用才能调用它:
struct Foo<'a> {
callbacks: Vec<&'a mut FnMut(u32)>,
}
Run Code Online (Sandbox Code Playgroud)