I have a problem trying to implement a callback to filter method in Rust. I have this function:
fn filter_by_mod(num: i32) -> fn(x: &i32) -> bool {
let func = |x: &i32| {
x.to_owned() % num == 0
};
return func;
}
I am trying to filter by mod and I would like to pass the number by paramenter and use in the func clousure, but when I try to compile I have the following error:
fn filter_by_mod(num: i32) -> fn(x: &i32) -> bool {
| ------------------- expected `for<'r> fn(&'r i32) -> bool` because of return type
9 | let func = |x: &i32| {
| ________________-
10 | | x.to_owned() % num == 0
11 | | };
| |_____- the found closure
12 | return func;
| ^^^^ expected fn pointer, found closure
|
= note: expected fn pointer `for<'r> fn(&'r i32) -> bool`
found closure `[closure@.\main.rs:9:16: 11:6]`
note: closures can only be coerced to `fn` types if they do not capture any variables
--> .\main.rs:10:24
|
10 | x.to_owned() % num == 0
| ^^^ `num` captured here
error: aborting due to previous error
How I can pass the number to func scope? I hope that you can understandme, because my english is not good. Thanks.