我无法从Result
返回函数的结果。每个教程都只展示了如何使用Result
,但没有展示如何从中返回一个值。
fn main(){
let mut a: Vec<String> = Vec::new();
a = gottem();
println!("{}", a.len().to_string());
//a.push(x.to_string()
}
async fn gottem() -> Result<Vec<String>, reqwest::Error> {
let mut a: Vec<String> = Vec::new();
let res = reqwest::get("https://www.rust-lang.org/en-US/")
.await?
.text()
.await?;
Document::from(res.as_str())
.find(Name("a"))
.filter_map(|n| n.attr("href"))
.for_each(|x| println!("{}", x));
Ok(a)
}
我遇到了以下错误:
error[E0308]: mismatched types
--> src/main.rs:13:9
|
13 | a = gottem();
| ^^^^^^^^ expected struct `std::vec::Vec`, found opaque type
...
18 | async fn gottem() -> Result<Vec<String>, reqwest::Error> {
| ----------------------------------- the `Output` of this `async fn`'s found opaque type
|
= note: expected struct `std::vec::Vec<std::string::String>`
found opaque type `impl std::future::Future`
async
(因为你试图使用它的方式是错误的),甚至是Rust中最基本的类型之一Result
,那么你可能在教程中错过了一些步骤。特别是这本书很好地介绍了Result
以及从中获取数据的不同方法。 - mcarton