简体   繁体   中英

How does rust simply get a return from a closure

Here's the problem I'm having with tauri.

'return' shows you the return value I need, and I know for a fact that writing it this way does not work at all.

'pick_file' is called asynchronously, and I know that message passing seems to work, but is there an easier way to get the value I need.

#[tauri::command]
fn open_file() -> String {
    dialog::FileDialogBuilder::default()
        .add_filter("data", &["json"])
        .pick_file(|path_buf| match path_buf {
            Some(p) => return format!("{}", p.to_str().unwrap()),
            _ => return "".into()
        });
}

First, return in a closure returns from the closure and not from the function that contains it.

The more fundamental issue is that you can't return a String from open_file() if you use FileDialogBuilder::pick_file() . According to the documentation , pick_file() is non-blocking and returns immediately without waiting for the user to pick the file. What you can do in the closure is send the file down a channel, and pick it up elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM