【发布时间】:2017-03-28 12:09:01
【问题描述】:
我已经学会了如何将闭包参数传递给函数,这样我就可以调用closure 两次:
let closure = || println!("hello");
fn call<F>(f: &F)
where
F: Fn(),
{
f();
}
call(&closure);
call(&closure);
当我使用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);
结果会报错:
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:_]`
我该如何解决?
【问题讨论】: