I was trying to use the file picker of tauri to read the contents of a file in Rust this way.
fn open_file() -> Option<String> {
let dialog_open_file = tauri::api::dialog::FileDialogBuilder::new();
dialog_open_file.pick_file(|path| {
match path {
Some(directory) => {
let archivo = fs::read_to_string(directory);
match archivo {
Ok(content) => { return Some(content); },
Err(error) => { return None; }
}
},
None => { return None; }
}
});
}
The problem is that when I try to return Some(content) I get the error "mismatched types expected unit type () found enum Option<String>"
error[E0308]: mismatched types
--> src/main.rs:25:43
|
25 | Ok(content) => { return Some(content); },
| ^^^^^^^^^^^^^ expected `()`, found enum
= note: expected unit type `()`
found enum `Option<String>`
error[E0308]: mismatched types
--> src/main.rs:18:19
|
18 | fn open_file() -> Option<String> {
| --------- ^^^^^^^^^^^^^^ expected enum `Option`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
|
= note: expected enum `Option<String>`
found unit type `()`
I have tried many ways but I can't return the Option out of the function "dialog_open_file" to make the function return something.