Hello,
I'm developing a website status monitor in Rust, but came across a problem with lifetimes. It's reproduced in this Rust Playground:
use reqwest::r#async::Client;
use serde::Deserialize;
use tokio::prelude::*;
fn main() {
let input = r#"{"sites":["http://rust-lang.org/"]}"#.to_owned();
let config: RequestConfig = serde_json::from_str(&input).unwrap();
let fetch = future::join_all(
config
.sites
.into_iter()
.map(|website| Client::new().get(website).send()),
)
.map(|r| println!("Success {}", r.len()))
.map_err(|err| println!("Error {}", err));
tokio::run(fetch);
}
#[derive(Debug, PartialEq, Deserialize)]
struct RequestConfig<'a> {
#[serde(borrow)]
sites: Vec<&'a str>,
}
To deserialize &'a str
, I followed the guide at Deserializer lifetimes · Serde. And tokio::run()
requires the future to be 'static
(/tokio/issues/431). It seems to me that lifetime of input
propagates to config
and fetch
, because it's borrowed in the async stream.
Error:
Compiling playground v0.0.1 (/playground)
error[E0597]: `input` does not live long enough
--> src/main.rs:7:54
|
7 | let config: RequestConfig = serde_json::from_str(&input).unwrap();
| ---------------------^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `input` is borrowed for `'static`
...
17 | }
| - `input` dropped here while still borrowed
How should I change my code to satisfy rustc?