Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. pub struct MyStruct {
  2. x: i64
  3. }
  4.  
  5. impl MyStruct {
  6. pub fn struct_function(&mut self, val: i64) {
  7. self.x += val;
  8. }
  9. }
  10.  
  11. fn normal_function(val: i64) {
  12. println!( "sum -> {}", val + 1);
  13. }
  14.  
  15. fn do_something_with_a_function(f: fn(i64)) {
  16. f(23);
  17. }
  18.  
  19. fn main() {
  20. // This works as you'd expect
  21. do_something_with_a_function(normal_function as fn(i64));
  22.  
  23. // What I'd like to do
  24. // This attempts to use a closure, but throws an error.
  25. let instance = MyStruct{x: 0};
  26. let instance_function = |val: i64|{instance.struct_function(val)};
  27. do_something_with_a_function(instance_function as fn(i64));
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement