简体   繁体   English

“不匹配的类型预期单元类型`()`找到枚举`选项<String> `&quot; 尝试使用 Tauri 读取文件内容时出错

[英]"mismatched types expected unit type `()` found enum `Option<String>`" Error when trying to read the content of a file with Tauri

I was trying to use the file picker of tauri to read the contents of a file in Rust this way.我试图使用 tauri 的文件选择器以这种方式读取 Rust 中文件的内容。

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> "问题是,当我尝试返回 Some(content) 时,我收到错误“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.我尝试了很多方法,但我无法从函数“dialog_open_file”中返回选项以使函数返回一些东西。

It looks like tauri has changed its API in this area.看起来tauri在这方面改变了它的 API。 See https://docs.rs/tauri-api/0.7.6/tauri_api/dialog/index.html .请参阅https://docs.rs/tauri-api/0.7.6/tauri_api/dialog/index.html Now the functions no longer accept a closure, but instead return a dialog::Response struct.现在这些函数不再接受闭包,而是返回一个dialog::Response结构。

This means your code could be written as something like:这意味着您的代码可以编写为:

// This is tauri's internal error type too.
use anyhow::{Error, bail};

fn open_file_impl() -> Result<String, Error> {
    use tauri::api::dialog::Response;
    let result = tauri::api::dialog::select(None,None)?;
    
    let path = match {
        Response::Okay(s) => s;
        Response::OkayMultiple(s) => bail!("multiple selected"),
        Response::Cancel => bail!("canceled");
    }
    Ok(fs::read_to_string(directory)?)
}

pub fn open_file() -> Option<String> {
   open_file_impl().ok()
}

I'd probably introduce a concrete error type with this, rather than relying on anyhow ... but that does make things longer.我可能会为此引入一个具体的错误类型,而不是anyhow都依赖......但这确实会使事情变得更长。

Or you could get rid of the open_file_impl altogether...或者你可以完全摆脱open_file_impl ...

fn open_file_impl() -> Option<String> {
    use tauri::api::dialog::Response;

    let result = tauri::api::dialog::select(None,None).ok()?;
    if let Response::Okay(path) = result {
        fs::read_to_string(directory).ok()
    } else {
        None
    }
}

Edit编辑

This post does not work for the OP question, because tauri's API requires the closure to be 'static , and thus rules out the possibility (except with dirty unsafe hacks) to retrieve the content of the file in a way similar to what was asked.这篇文章不适用于 OP 问题,因为 tauri 的 API 要求闭包是'static ,因此排除了以类似于所要求的方式检索文件内容的可能性(除了肮脏的unsafe黑客)。

Also, tauri's API has been changed in more recent versions.此外,tauri 的 API 在最近的版本中已更改。 If you are only interested in a solution that specifically works with tauri, please see @Michael Anderson's answer.如果您只对专门适用于 tauri 的解决方案感兴趣,请参阅@Michael Anderson 的回答。


As you can see, there are two errors, which are opposite, in some way: one error tells you that Rust expected () , but found Option<String> , and the other one that it expected Option<String> , but found () .正如你所看到的,有两个错误,在某种程度上是相反的:一个错误告诉你 Rust 期望() ,但是找到了Option<String> ,另一个错误告诉你它期望Option<String> ,但是找到了() . This should prompt you to check if you are passing you Option<String> at the right place and, in fact, you're not.这应该会提示您检查是否在正确的位置传递Option<String> ,事实上,您不是。

A closure is a function (not quite the same as other functions you define with fn , but still a function), so when you call return in it, you're actually returning inside the closure.闭包一个函数(与您使用fn定义的其他函数不完全相同,但仍然是一个函数),因此当您在其中调用return时,您实际上是在闭包内部返回。 So, the error just comes from the fact that pick_file expected a function that returns () , whereas you provided one that returns Option<String> , and conversely the outer function is (implicitly) returning () .因此,错误只是来自这样一个事实,即pick_file期望一个返回()的函数,而您提供了一个返回Option<String>的函数,相反,外部函数是(隐式)返回()

You may ask: is there a way to return from inside the closure, but for the return statement to apply to the outside function, and the answer is no , so one way would be to create a variable outside and mutate it from inside the closure:你可能会问:有没有办法从闭包内部返回,但是 return 语句应用于外部函数,答案是否定的,所以一种方法是在外部创建一个变量并从闭包内部对其进行变异:

fn open_file() -> Option<String> {
    let dialog_open_file = tauri::api::dialog::FileDialogBuilder::new();
    let mut result = None;
    dialog_open_file.pick_file(|path| {
        if let Some(directory) = path {
            let archivo = fs::read_to_string(directory);
            if let Ok(content) = archivo {
                result = Some(content);
            }
        }
    });
    result
}

If you format your code, you would find out that you are missing the return statement in the outer space.如果你格式化你的代码,你会发现你错过了外层空间的 return 语句。

fn open_file() -> Option<String> {
    let dialog_open_file = tauri::api::dialog::FileDialogBuilder::new();
    let mut result = None;
    dialog_open_file.pick_file(|path| match path {
        Some(directory) => {
            let archivo = fs::read_to_string(directory);
            match archivo {
                Ok(content) => result = Some(content),
                Err(error) => result = None,
            }
        }
    });
    result
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不匹配:在分配字符串时预期&str找到了字符串 - Mismatched types: expected &str found String when assigning string 预期的单元类型“()”,找到“enum std::option::Option” - Expected unit type '()', found 'enum std::option::Option' 不匹配的类型错误:预期的 `char`,找到参考 - Mismatched types error: expected `char`, found reference if let语句中的类型不匹配:找到期望结果 - Mismatched types in if let statement: found Option expected Result 错误 [E0308]: 不匹配的类型 — 预期为 `&amp;str`,找到结构体 `std::string::String` - error[E0308]: mismatched types — expected `&str`, found struct `std::string::String` 错误:类型不匹配:尝试实现冒泡排序时,出现了预期的&#39;usize&#39;,并出现了&#39;&usize&#39; - error: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort 使用本地的期货执行器时,为什么会出现类型不匹配的错误(预期为mpsc :: Receiver,找到())? - Why do I get a mismatched type error (expected mpsc::Receiver, found ()) when using a local Futures executor? Rust“预期类型”错误打印完全相同的不匹配类型 - Rust "expected type" error prints mismatched types that are exactly the same Rust 预期类型不匹配 (),找到结构`Enumerate - Rust mismatched types expected (), found struct `Enumerate 不匹配的类型需要 `i8`,发现 `()` - mismatched types expected `i8`, found `()`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM