Is there a way to create a function pointer to a method in Rust?

With fully-qualified syntax, Foo::bar will work, yielding a fn(&Foo) -> () (similar to Python).

let callback = Foo::bar;
// called like
callback(&foo);

However, if you want it with the self variable already bound (as in, calling callback() will be the same as calling bar on the foo object), then you need to use an explicit closure:

let callback = || foo.bar();
// called like
callback();

Leave a Comment