I want to make a global function pointer pointing to a class method, so I did something like the following minimal reproducible example:
struct Foo<'a> {
data: &'a str,
}
impl<'a> Foo<'a> {
pub fn foo(&self) {
println!("{}", self.data);
}
}
type FooFn = fn(&Foo);
const FUNC: FooFn = Foo::foo;
fn main() {
let data = String::from("hello");
let foo = Foo { data: &data };
FUNC(&foo);
}
But rustc gives me this error:
$ rustc test.rs
error[E0308]: mismatched types
--> test.rs:13:21
|
13 | const FUNC: FooFn = Foo::foo;
| ^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r Foo<'s>)`
found fn pointer `for<'r> fn(&'r Foo<'_>)`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
What is this error trying to say? How can I fix it?