我发现很难理解以下代码有什么问题。我得到了预期的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?
发布评论
评论(1)
回想一下
?
操作员通过传播err <
结果 (或option
) /code>(或无
)从当前功能出发。因此,此表达式:具有类型
vec&lt; u8&gt;
作为?
操作员已解开结果
。如果要自己处理错误,请省略?
操作员:Recall that the
?
operator unwraps aResult
(orOption
) by propagating theErr
(orNone
) case out of the current function. Therefore, this expression:Has type
Vec<u8>
as the?
operator has unwrapped theResult
. If you want to handle errors yourself, then omit the?
operator: