Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borrowed value does not live long enough with async funtion

I am new to Rust and trying to make some code async to run a bunch of tasks in parallel. Here is a simplified example:

use futures::future::join_all;

#[tokio::main]
async fn main() {
    let mut list = Vec::new();
    for i in 1..10 {
        let my_str = format!("Value is: {:?}", &i);
        let future = do_something(&my_str);
        list.push(future);
    }
    join_all(list).await;
}

async fn do_something(value: &str)
{
    println!("value is: {:?}", value);
}

This fails with "Borrowed value does not live long enough" on the do_something(&my_str) call. I can get the code to compile by changing do_something to accept a String instead of an &str. However, it seems a bit strange to require a String when an &str would work. Is there a better pattern to use here? Thanks!

like image 432
Daniel Avatar asked Jan 26 '26 09:01

Daniel


1 Answers

"However, it seems a bit strange to require a String when an &str would work." But an &str can't work here because it only borrows my_str which gets destroyed before the future completes:

for i in 1..10 {
    // Create a new `String` and store it in `my_str`
    let my_str = format!("Value is: {:?}", &i);
    // Create a future that borrows `my_str`. Note that the future is not
    // yet started
    let future = do_something(&my_str);
    // Store the future in `list`
    list.push(future);
    // Destroy `my_str` since it goes out of scope and wasn't moved.
}
// Run the futures from `list` until they complete. At this point each
// future will try to access the string that they have borrowed, but those
// strings have already been freed!
join_all(list).await;

Instead your do_something should take ownership of the string along with responsibility for freeing it:

use futures::future::join_all;

#[tokio::main]
async fn main() {
    let mut list = Vec::new();
    for i in 1..10 {
        // Create a new `String` and store it in `my_str`
        let my_str = format!("Value is: {:?}", &i);
        // Create a future and _move_ `my_str` into it.
        let future = do_something(my_str);
        // Store the future in `list`
        list.push(future);
        // `my_str` is not destroyed since it was moved into the future.
    }
    join_all(list).await;
}

async fn do_something(value: String)
{
    println!("value is: {:?}", value);
    // Destroy `value` since it goes out of scope and wasn't moved.
}
like image 74
Jmb Avatar answered Jan 29 '26 02:01

Jmb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!