Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Get Rust Member Function to Callback in Struct

I’ve been trying to understand callback functions in Rust. After going through a few examples in stackoverflow, I still can’t get it to run when the function I want to call is a member function of another struct.

My example is built upon this example post: How to pass a member function of a struct to another struct as callback

struct Person {
    name: String
}

impl Person {
    pub fn print_name(&self) {
        println!("Hello from {}", self.name);
    }
}

struct Operation {
    cb: fn(&Person),
}

impl Operation {
    pub fn caller(&self) {
        (self.cb)()
    }
}

fn main() {
    let person = Person {
        name: "Brad".to_string()
    };

    let op = Operation {
        cb: Person::print_name,
    };
    
    op.caller()
}

This example doesn’t break the linter, but when I try to run it, I get errors about the type and arguments provided.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   Compiling playground v0.0.1 (/playground)
error[E0061]: this function takes 1 argument but 0 arguments were supplied
  --> src/main.rs:17:9
   |
17 |         (self.cb)()
   |         ^^^^^^^^^-- supplied 0 arguments
   |         |
   |         expected 1 argument

For more information about this error, try `rustc --explain E0061`.
error: could not compile `playground` due to previous error

It seems to me that it cant resolve the self variable so it makes me wonder why the example in the previous post was accepted if that structs function can’t actually be called.

>Solution :

In your struct definition, you are saying that the function stored in cb takes a &Person argument, so you need to make sure that argument is passed along whenever you try to call the function. You should change the caller method to handle the argument like so:

struct Person {
    name: String
}

impl Person {
    pub fn print_name(&self) {
        println!("Hello from {}", self.name);
    }
}

struct Operation {
    cb: fn(&Person),
}

impl Operation {
    pub fn caller(&self, person: &Person) {
        (self.cb)(person)
    }
}

fn main() {
    let person = Person {
        name: "Brad".to_string()
    };

    let op = Operation {
        cb: Person::print_name,
    };
    
    op.caller(&person)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading