A tokio::spawn related lifetime issue

12 Views Asked by At

If I want to use borrowed parameters in tokio::spwan, as in the following function, how to solve the error of "borrowed data escapes outside of function"?

async fn error(param: &str){
    let _ = tokio::spawn(async move {
        println!("string is {:?}", param);
    }).await;
}

full error

error[E0521]: borrowed data escapes outside of function
  --> src/main.rs:11:13
   |
10 |   async fn error(param: &str){
   |                  -----  - let's call the lifetime of this reference `'1`
   |                  |
   |                  `param` is a reference that is only valid in the function body
11 |       let _ = tokio::spawn(async move {
   |  _____________^
12 | |         println!("string is {:?}", param);
13 | |     }).await;
   | |      ^
   | |      |
   | |______`param` escapes the function body here
   |        argument requires that `'1` must outlive `'static`

Since the database connection is passed as a borrowed parameter, I'm not quite sure how this can be resolved...

0

There are 0 best solutions below