fn foo()->结果<()>抛出“期望的2类型参数” [英] fn foo() -> Result<()> throws "expected 2 type arguments"

查看:86
本文介绍了fn foo()->结果<()>抛出“期望的2类型参数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在编译这段Rust代码时不允许 Result<())

Why isn't Result<()> allowed when compiling this bit of Rust code? Is it a breaking change between Rust editions?

fn run() -> Result<()> {
    let (tx, rx) = channel();

    thread::spawn(move || {
        do_things_with_tx(&exit_tx);
    });

    match exit_rx.recv() {
        Ok(result) => if let Err(reason) = result {
            return Err(reason);
        },
        Err(e) => {
            return Err(e.into());
        },
    }

    Ok(())
}

编译器说:

error[E0107]: wrong number of type arguments: expected 2, found 1
    --> src/main.rs:1000:18
     |
1000 | fn run_wifi() -> Result<()> {
     |                  ^^^^^^^^^^ expected 2 type arguments

当我将返回类型调整为 Result<(),Err> ,它说:

When I tweak the return type to Result<(), Err>, it says:

error[E0107]: wrong number of type arguments: expected 2, found 0
    --> src/main.rs:1000:29
     |
1000 | fn run() -> Result<(), Err> {
     |                        ^^^ expected 2 type arguments

这来自 wifi连接项目

推荐答案

Result 的定义一直以来都是以下内容:

The definition of Result is, and has always been, the following:

pub enum Result<T, E> {
    Ok(T),
    Err(E),
}

甚至在 Rust编程语言,展示它的简单性。作为 OK 结果和 error 结果的通用求和类型,它总是期望两个类型参数,并且如果编译器无法推断出它们,或者它们的列表,编译器会抱怨。类型参数没有预期的长度。

This definition is even presented in the Rust Programming language, to show how simple it is. As a generic sum type of an OK outcome and an error outcome, it always expects two type parameters, and the compiler will complain if it cannot infer them, or the list of type arguments does not have the expected length.

另一方面,可能会发现许多显示 Result 和一个类型参数,如 Result<(()>

On the other hand, one may find many libraries and respective docs showing a Result with a single type argument, as in Result<()>. What gives?

这仍然不是魔术。按照惯例,库在包装箱或模块级别为结果类型创建类型 aliases 。这样效果很好,因为通常会产生相同的本地创建类型的错误。

It's still no magic. By convention, libraries create type aliases for result types at the level of a crate or module. This works pretty well because it is common for those to produce errors of the same, locally created type.

pub type Result<T> = Result<T, Error>;

事实上,它很普遍,以至于箱子 错误链 (许多错误类型的辅助工具之一)会自动使用 error_chain!宏创建此定义。
这样,如果您在使用错误链例如 wifi-connect ),或者使用的库可能或可能不使用错误链,则您应该假设提及 Result< T> 是本地类型别名到特定于域的 Result< T,Error> 。如有疑问,请在生成的文档页面中单击该类型,将您带到具体的定义(在本例中为别名)。

In fact, it is so common that the crate error-chain, one of the many error type helper crates, automatically creates this definition when using the error_chain! macro. As such, if you are in a project using error-chain (such as wifi-connect), or using a library that may or may not use error-chain, you are expected to assume that mentions of Result<T> are local type aliases to a domain-specific Result<T, Error>. In case of doubt, clicking on that type in the generated documentation pages will direct you to the concrete definition (in this case, the alias).

这篇关于fn foo()-&gt;结果&lt;()&gt;抛出“期望的2类型参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆