相关疑难解决方法(0)

结果没有名为"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
}
Run Code Online (Sandbox Code Playgroud)

错误是:

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`
Run Code Online (Sandbox Code Playgroud)

rust

18
推荐指数
1
解决办法
2675
查看次数

在不同的模块中使用无方法特征实现

我有lib.rs:

pub mod genetics {
  use std;

  pub trait RandNewable {
    fn new() -> Self;
  }

  pub trait Gene : std::fmt::Debug + Copy + Clone + RandNewable {}

  #[derive(Debug, Copy, Clone)]
  pub struct TraderGene {
    cheat: bool,
  }

  impl RandNewable for TraderGene {
    fn new() -> TraderGene {
      TraderGene {
        cheat: true,
      }
    }
  }

  #[derive(Debug)]
  pub struct DNA<G: Gene> {
    genes: [G; 3],
  }

  impl <G: Gene> DNA<G> {
    pub fn create() -> DNA<G> {
      DNA {
        genes: [RandNewable::new(), …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
223
查看次数

标签 统计

rust ×2