According to the docs on task::spawn (note I'm using tokio::spawn which seems similar but lacks the description)
The
'static
constraint means that the closure and its return value must have a lifetime of the whole program execution. The reason for this is that threads can detach and outlive the lifetime they have been created in. Indeed if the thread, and by extension its return value, can outlive their caller, we need to make sure that they will be valid afterwards, and since we can't know when it will return we need to have them valid as long as possible, that is until the end of the program, hence the'static
lifetime.
I'm trying to pass a Url
into a thread, like this,
do_update(confg.to_feed_url()).await;
Where I do this in do_update
,
async fn do_update(url: Url) {
let task = task::spawn(async {
let duration = Duration::from_millis(5_000);
let mut stream = time::interval(duration);
stream.tick().await;
loop {
feeds::MyFeed::from_url(url.clone(), true);
This doesn't work and generates these errors,
error[E0373]: async block may outlive the current function, but it borrows
url
, which is owned by the current function
I've read the docs, but this doesn't much make sense to me because I'm cloning in the block. How can I resolve this error?
Arc
and move its clone into the closure. – user4815162342