Dancing and learning/singing (Rust async example)

I am reading the Asynchronous Programming in Rust book. As the book said: "both learning and singing can happen at the same time as dancing". Following the example, I wrote the simple code to test it. However in my code dancing is always happen after learning and signing. why?


Async/await runs things concurrently by repeatedly swapping out the currently running task, however such swapping can only happen at an .await. Since learn_song and sing_song have no .await, no such swapping of current task can happen while they run.

This is also why you should never sleep using thread::sleep. Rust is not able to let some other task run while it is sleeping because there is no .await.

Check out this example: playground. Additionally try increasing the loop in dancing to run 20 times.

1 Like

Without using tokio, how can I yield a task?

You are not going to be able to do anything useful without an executor. The yield function should be available in whatever executor you are using.

I can't find anything equivalent in Rust's Futures.

It doesn't have one. The futures crate does not provide a full featured executor.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.