Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct MyStruct {
- x: i64
- }
- impl MyStruct {
- pub fn struct_function(&mut self, val: i64) {
- self.x += val;
- }
- }
- fn normal_function(val: i64) {
- println!( "sum -> {}", val + 1);
- }
- fn do_something_with_a_function(f: fn(i64)) {
- f(23);
- }
- fn main() {
- // This works as you'd expect
- do_something_with_a_function(normal_function as fn(i64));
- // What I'd like to do
- // This attempts to use a closure, but throws an error.
- let instance = MyStruct{x: 0};
- let instance_function = |val: i64|{instance.struct_function(val)};
- do_something_with_a_function(instance_function as fn(i64));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement