gotcharustModerate
Why does passing a closure to function which accepts a function pointer not work?
Viewed 0 times
pointerfunctionwhydoesclosureacceptsnotwhichworkpassing
Problem
In the second edition of The Rust Programming Language (emphasis mine):
Function pointers implement all three of the closure traits (
Passing a closure to a function which accepts a function pointer as an argument doesn't compile:
Why does this not work?
Function pointers implement all three of the closure traits (
Fn, FnMut, and FnOnce), so you can always pass a function pointer as an argument for a function that expects a closure. It’s best to write functions using a generic type and one of the closure traits so your functions can accept either functions or closures. Passing a closure to a function which accepts a function pointer as an argument doesn't compile:
fn main() {
let a = String::from("abc");
let x = || println!("{}", a);
fn wrap(c: fn() -> ()) -> () {
c()
}
wrap(x);
}error[E0308]: mismatched types
--> src/main.rs:10:10
|
10 | wrap(x);
| ^ expected fn pointer, found closure
|
= note: expected type fn()
found type [closure@src/main.rs:4:13: 4:33 a:_]
Why does this not work?
Solution
A closure isn't a function.
You can pass a function to a function expecting a closure, but there's no reason for the reverse to be true.
If you want to be able to pass both closures and functions as argument, just prepare it for closures.
For example:
You can pass a function to a function expecting a closure, but there's no reason for the reverse to be true.
If you want to be able to pass both closures and functions as argument, just prepare it for closures.
For example:
let a = String::from("abc");
let x = || println!("x {}", a);
fn y() {
println!("y")
}
fn wrap(c: impl Fn()) {
c()
}
wrap(x); // pass a closure
wrap(y); // pass a functionCode Snippets
let a = String::from("abc");
let x = || println!("x {}", a);
fn y() {
println!("y")
}
fn wrap(c: impl Fn()) {
c()
}
wrap(x); // pass a closure
wrap(y); // pass a functionContext
Stack Overflow Q#52696907, score: 43
Revisions (0)
No revisions yet.