结果类型不在名为`unwrap`的作用域中实现方法

由于某些原因,Rust编译器抱怨Result没有实现unwrap ,即使我提供的Error类型没有实现Debug。 下面提供了错误代码。

use std::fmt::{Display, Debug};
use std::error::Error;

trait MyError: Error + Display + Debug {}
type MyResult<T> = Result<T, MyError>;

trait Foo: Clone {}

trait MyTrait {
    fn my_function<T: Foo>(&self) -> MyResult<T>;

    fn unwrap_function<T: Foo>(&self) -> T {
        self.my_function().unwrap()
    }
}

当你定义你的类型

type MyResult<T> = Result<T, MyError>;

您实际上将您的类型定义为MyError类型,因为MyError不是具体的类型,而是一个特征。 但是, Result<T, E>状态

impl<T, E> Result<T, E> where E: Debug {
    /* ... */
}

这隐含地要求E是一个大小的类型。 因此,在你的情况下,实际情况是无效的和不可用的(实际上,如果不是所有Result<T, E>需要调整TE大小,所以未确认的Result不是很有用) 。

在你的案例中最简单的修复就是把你的错误放在一个Box ,就像这样:

type MyResult<T> = Result<T, Box<MyError>>;
链接地址: http://www.djcxy.com/p/85675.html

上一篇: Result type does not implement method in scope named `unwrap`

下一篇: How to download file from Google Drive using Drive.API?