我想制作一个指向类方法的全局函数指针,所以我做了如下类似的事情:
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);
}
但是rustc
给了我一个错误:
$ 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`.
这个错误想说什么?我该怎么修?