How can I access everything inside this function inside a main function when this function is called inside the main?

Hi

How can I access everything inside this function inside a main function when this function is called inside the main?

fn on_message() -> Callbacks<'a, T> {
let mut mc = m.callbacks(());
mc.on_message(|_,msg| {
    if ! msg.retained() {
        if bonzo.matches(&msg) {
            println!("bonzo {:?}",msg);
        } else
        if frodo.matches(&msg) {
            println!("frodo {:?}",msg);
        }
    }
mc
});

}

Link to the mqtt client library I use: GitHub - stevedonovan/mosquitto-client

Thanks

Who is going to access what? The main function can't do anything while a function it calls is on the stack, and you can access anything you like with a debugger. So your question doesn't make much sense as is.

Inside main this function is called and main will have access to everything inside this function.

What is "everything"? The only thing inside that function is a Callbacks<'a, T>, which isn't in that function once it is returned, so what exactly do you want to do?

I want the callback to be executed once the function is called in main.

Write it as a closure inside main:

fn main() {
    let on_message = || -> Callbacks<'a, T> {
        let mut mc = m.callbacks(());
        mc.on_message(|_,msg| {
            if ! msg.retained() {
                if bonzo.matches(&msg) {
                    println!("bonzo {:?}",msg);
                } else
                if frodo.matches(&msg) {
                    println!("frodo {:?}",msg);
                }
            }
        mc
    });
}

Does that mean I no longer need to call the on_message function in main?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.