结果没有名为“unwrap()"的方法? [英] Result has no method called "unwrap()"?

查看:33
本文介绍了结果没有名为“unwrap()"的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多么奇怪的错误:

use std::collections::BTreeMap;

struct MyStruct1;
struct Error;

fn get_res() -> Result<(MyStruct1, BTreeMap<String, String>), Error> {
    Err(Error)
}

fn main() {
    let res1 = get_res();
    assert!(res1.is_ok());
    assert_eq!("just for test", res1.unwrap()); //error
}

错误是:

error: no method named `unwrap` found for type `std::result::Result<(MyStruct1, std::collections::BTreeMap<std::string::String, std::string::String>), Error>` in the current scope
  --> src/main.rs:13:38
   |
13 |     assert_eq!("just for test", res1.unwrap()); //error
   |                                      ^^^^^^
   |
   = note: the method `unwrap` exists but the following trait bounds were not satisfied: `Error : std::fmt::Debug`

推荐答案

如果您阅读了 Result::unwrap,你会注意到它在一个叫做:

If you read the documentation for Result::unwrap, you'll note that it's under a little section called:

impl<T, E> Result<T, E> 
    where E: Debug

这意味着该部分中的方法只存在,只要满足给定的约束.

This means the methods in that section only exist so long as the given constraints are satisfied.

unwrap 不存在的唯一原因是 Error 没有实现 Debug.

The only reason unwrap wouldn't exist is that Error doesn't implement Debug.

这篇关于结果没有名为“unwrap()"的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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