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?
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);