I'm new to Rust, and trying to learn the language by creating something around it.
Facing this problem that the self
is valid inside this function but the closure is async
so it may be executed after the lifetime of this function and that closure captures self
. I get the problem. But what should I do? I understand that it is something to do with the lifetimes. But can someone point to me what the solution is?
pub async fn run(&mut self) -> Result<i32, &str> {
.
.
.
match server.accept().await {
Ok(p) => {
tokio::spawn(async {
self.handle_temp_connection(p)
});
},
Err(err) => {
println!("Some problem accepting a connection.")
}
}
}
fn handle_temp_connection(&self, p_socket_addr: (TcpStream, SocketAddr)) {
...
}
Error:
error[E0521]: borrowed data escapes outside of associated function
--> src/app.rs:34:21
|
20 | pub async fn run(&mut self) -> Result<i32, &str> {
| ---------
| |
| `self` is a reference that is only valid in the associated function body
| let's call the lifetime of this reference `'1`
...
34 | / tokio::spawn(async {
35 | | self.handle_temp_connection(p)
36 | | });
| | ^
| | |
| |______________________`self` escapes the associated function body here
| argument requires that `'1` must outlive `'static`