我想编写一个以闭包为参数的函数,该函数以另一个闭包为参数,如下所示:
fn test<F: Fn(G), G: Fn()>(f: F) {
let print = || {
println!("hello");
};
f(print);
}
fn main() {
test(|print| {
print();
})
}
但这会给出以下错误:
error[E0308]: mismatched types
--> src/main.rs:5:7
|
1 | fn test<F: Fn(G), G: Fn()>(f: F) {
| - expected this type parameter
2 | let print = || {
| -- the found closure
...
5 | f(print);
| - ^^^^^ expected type parameter `G`, found closure
| |
| arguments to this function are incorrect
|
= note: expected type parameter `G`
found closure `{closure@src/main.rs:2:17: 2:19}`