I cannot return a result of a function from a Result
. Every tutorial only shows how to use a Result, but not how to return a value from it.
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)
}
I get the following error:
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
(because you can't use it the way you are trying to use it) or evenResult
, one of the most fundamental Rust type, you've probably missed a few steps in your tutorials. In particular, the book coversResult
and the different ways to get data out of it pretty well. – Greenock