I am struggling to learn Rust with his lifetime. So I am making the method bellow in a web client wrapper.
pub async fn get_as_bytes<T: serde::Deserialize>(
&self,
url: &str,
headers: Option<HeaderMap>,
) -> Result<T, Box<dyn Error>> {
let mut req = self.reqwest_client.request(reqwest::Method::GET, url);
if let Some(hds) = headers {
req = req.headers(hds);
}
let resp: Response = req.send().await?;
let full = resp.bytes().await?;
let result = serde_json::from_slice(&full).unwrap();
Ok(result)
}
When I try to compile I get that:
error[E0106]: missing lifetime specifier
--> src/webclient.rs:53:41
|
53 | pub async fn get_as_bytes<T: serde::Deserialize>(&self, url: &str, headers: Option<HeaderMap>) -> Result<T, Box<dyn Error>> {
| ^^^^^^^^^^^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
53 | pub async fn get_as_bytes<'a, T: serde::Deserialize<'a>>(&self, url: &str, headers: Option<HeaderMap>) -> Result<T, Box<dyn Error>> {
| ^^^ ^^^^^^^^^^^^^^^
error[E0597]: `full` does not live long enough
--> src/webclient.rs:63:45
|
63 | let result = serde_json::from_slice(&full).unwrap();
| -----------------------^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `full` is borrowed for `'static`
64 | Ok(result)
65 | }
| - `full` dropped here while still borrowed
How do I make &full
last since I am trying to return it?