结果没有名为" unwrap()"?的方法

时间:2015-06-23 05:02:22

标签: rust

多么奇怪的错误:

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`

1 个答案:

答案 0 :(得分:15)

如果你阅读了Result::unwrap的文档,你会注意到它位于一个名为的小部分下面:

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

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

unwrap不存在的唯一原因是Error未实现Debug