作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,
struct Foo;
impl Foo {
fn bar(&self) {}
fn baz(&self) {}
}
fn main() {
let foo = Foo;
let callback = foo.bar;
}
error[E0615]: attempted to take value of method `bar` on type `Foo`
--> src/main.rs:10:24
|
10 | let callback = foo.bar;
| ^^^ help: use parentheses to call the method: `bar()`
最佳答案
与 fully-qualified syntax , Foo::bar
将起作用,产生一个 fn(&Foo) -> ()
(类似于 Python)。
let callback = Foo::bar;
// called like
callback(&foo);
但是,如果您希望它与已经绑定(bind)的 self
变量一起使用(例如,调用 callback()
将与调用 bar
在 foo
对象上),那么你需要使用显式闭包:
let callback = || foo.bar();
// called like
callback();
关于rust - 有没有办法在 Rust 中创建指向方法的函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24728394/
我是一名优秀的程序员,十分优秀!