Greetings !
I am trying to piece out this piece of borrowing puzzle :
I have a function with this signature :
async fn download_stars<T: AsRef<str>>(
link: T,
mut stop_condition: impl FnMut(Option<&str>) -> bool,
) -> Result<Vec<Query>, reqwest::Error> {
// Some code elided
let mut stars: Vec<Query> = Vec::new();
let mut next_link: Option<&str> = Some(link.as_ref());
while stop_condition(next_link) {
if let Some(link) = next_link {
let res = client.get(&*link).send().await?;
let headers = &res.headers();
next_link = extract_link_next(headers);
let mut s = res.json().await?;
stars.append(&mut s);
}
}
Ok(stars)
}
And of course, the Rust compiler sends back :
error[E0597]: `res` does not live long enough
--> src/main.rs:58:28
|
55 | while stop_condition(next_link) {
| --------- borrow later used here
...
58 | let headers = &res.headers();
| ^^^ borrowed value does not live long enough
...
62 | }
| - `res` dropped here while still borrowed
error[E0505]: cannot move out of `res` because it is borrowed
--> src/main.rs:60:25
|
55 | while stop_condition(next_link) {
| --------- borrow later used here
...
58 | let headers = &res.headers();
| --- borrow of `res` occurs here
59 | next_link = extract_link_next(headers);
60 | let mut s = res.json().await?;
| ^^^ move out of `res` occurs here
error: aborting due to 2 previous errors; 1 warning emitted
Which seems totally fair but how would I solve it ?