Skip to content

same async recursion code using tokio::task::{spawn/spawn_local}, but one of them can't compile while the other pass compiler check. #5230

Answered by Darksonn
aj3n asked this question in Q&A
Discussion options

You must be logged in to vote

I don't know why it only happens for one of them, but the easiest way to solve this is to wrap the tokio::spawn call in a non-async function.

fn spawn_tokio_recure(cnt: i32) -> JoinHandle<()> {
    tokio::task::spawn(tokio_recure(cnr))
}

async fn tokio_recure(cnt: i32) {
	if cnt > 0 {
		let j = spawn_tokio_recure(cnt - 1);
		j.await.unwrap()
	}
}

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@aj3n
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@aj3n
Comment options

@Noah-Kennedy
Comment options

Answer selected by aj3n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants