预期的struct`vec',找到了枚举``

发布于 2025-01-23 21:59:02 字数 977 浏览 0 评论 0 原文

我发现很难理解以下代码有什么问题。我得到了预期的struct VEC,找到枚举结果 ok ok(from_cache)的错误,但是我已经从

在网络刮擦期间,我正在尝试缓存缓存中URL的内容,并试图重新使用它。

use std::error::Error;

#[tokio::main]
async fn main() ->  Result<(), Box<dyn Error>> {
    let url: &str = "https://example.com/";
    let html = match cacache::read("./cache", url).await? {
        Ok(from_cache) => String::from_utf8(from_cache),
        Err(_) => {
            let t_html = reqwest::get(url).await?.text().await?;
            cacache::write("./cache", url, &t_html).await?;
            t_html
        },
    };
    println!("html = {:?}", html);

    Ok(())
}

这是

I find it difficult to understand what's wrong with the below code. I'm getting expected struct Vec, found enum Result error at Ok(from_cache), but I have adopted the code from https://github.com/platy/update-tracker/blob/843092708906063704442f352231bfbac5b06196/server/src/web/mod.rs#L216-L226

During web scraping, I'm trying to cache the content of the URL in the cache and trying to reuse it.

use std::error::Error;

#[tokio::main]
async fn main() ->  Result<(), Box<dyn Error>> {
    let url: &str = "https://example.com/";
    let html = match cacache::read("./cache", url).await? {
        Ok(from_cache) => String::from_utf8(from_cache),
        Err(_) => {
            let t_html = reqwest::get(url).await?.text().await?;
            cacache::write("./cache", url, &t_html).await?;
            t_html
        },
    };
    println!("html = {:?}", html);

    Ok(())
}

Here's the playground (but, it shows other errors due to missing dependencies). Can anyone please explain this or share any relevant guide to gather more information about this topic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2025-01-30 21:59:02

回想一下 操作员通过传播 err < 结果 (或 option ) /code>(或)从当前功能出发。因此,此表达式:

cacache::read("./cache", url).await?

具有类型 vec&lt; u8&gt; 作为操作员已解开结果。如果要自己处理错误,请省略操作员:

cacache::read("./cache", url).await

Recall that the ? operator unwraps a Result (or Option) by propagating the Err (or None) case out of the current function. Therefore, this expression:

cacache::read("./cache", url).await?

Has type Vec<u8> as the ? operator has unwrapped the Result. If you want to handle errors yourself, then omit the ? operator:

cacache::read("./cache", url).await
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文