【问题标题】:no implementation for `dyn std::error::Error == dyn std::error::Error``dyn std::error::Error == dyn std::error::Error 没有实现
【发布时间】:2022-06-16 00:31:00
【问题描述】:

我有一个异步函数,它在所有 Web 解析完成后返回一个结构。运行测试时出现错误:no implementation for `dyn std::error::Error == dyn std::error::Error` 。我该如何解决这个问题或我做错了什么?

#[derive(Debug, PartialEq, Eq)]
pub struct ParsingResult<'a> {
    title: &'a str,
    content: String,
}
impl<'a> ParsingResult<'a> {
    pub fn new(title: &'a str, content: String) -> Self {
        ParsingResult { title, content }
    }
}
pub async fn yahoo_template<'a>(
    link_to_the_article: &str,
) -> Result<ParsingResult<'a>, Box<dyn error::Error>> {
    let resp = reqwest::get(link_to_the_article).await?.text().await?;

    let mut final_res: Vec<String> = Vec::new();
    Document::from(resp.as_str())
        .find(Name("a"))
        .filter_map(|n| n.attr("href"))
        .for_each(|x| {
            if x.contains("http") || x.contains("https") {
                final_res.push(String::from(x));
            }
        });
    // println!("{:?}", final_res);
    Ok(ParsingResult::new("Title", String::from("String")))
}

#[cfg(test)]
mod templates_tests {
    use super::*;
    #[tokio::test]
    async fn make_it_work() {
        let expected = Ok(ParsingResult::new("Title", String::from("String")));
        assert_eq!(
            expected,
            yahoo_template("https://ro.wikipedia.org/wiki/Guy_de_Maupassant").await
        );
    }
}

【问题讨论】:

  • 请始终从cargo check 发布完整错误。

标签: rust async-await rust-tokio actix-web cargo


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多