How can I pass a FnMut closure to a function using a reference in Rust?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:32:45

问题


I've learned how to pass a closure argument to a function so I can call closure twice:

let closure = || println!("hello");
fn call<F>(f: &F)
where
    F: Fn(),
{
    f();
}
call(&closure);
call(&closure);

When I use FnMut:

let mut string: String = "hello".to_owned();
let change_string = || string.push_str(" world");
fn call<F>(mut f: &mut F)
where
    F: FnMut(),
{
    f();
}
call(&change_string);
call(&change_string);

It will turn out an error:

error[E0308]: mismatched types
  --> src/main.rs:10:10
   |
10 |     call(&change_string);
   |          ^^^^^^^^^^^^^^ types differ in mutability
   |
   = note: expected type `&mut _`
              found type `&[closure@src/main.rs:3:25: 3:53 string:_]`

How can I solve it?


回答1:


As the error message says:

expected type `&mut _`
   found type `&[closure@src/main.rs:3:25: 3:53 string:_]`

It is expecting a mutable reference to something (&mut _), but you are providing an immutable reference to a closure (&...). Take a mutable reference:

call(&mut change_string);

Which leads to the next error:

error: cannot borrow immutable local variable `change_string` as mutable
 --> src/main.rs:9:15
  |
3 |     let change_string = || string.push_str(" world");
  |         ------------- use `mut change_string` here to make mutable
...
9 |     call(&mut change_string);
  |               ^^^^^^^^^^^^^ cannot borrow mutably

Taking a mutable reference requires that the value itself be mutable:

let mut change_string = || string.push_str(" world");

In this case, you don't need to take a &mut F at all, as FnMut is implemented for mutable references to FnMut. That is, this works:

fn call(mut f: impl FnMut()) {
    f();
}

call(&mut change_string);
call(&mut change_string);


来源:https://stackoverflow.com/questions/43069178/how-can-i-pass-a-fnmut-closure-to-a-function-using-a-reference-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!