Questions related to Async

code show as below:

fn main() {
    println!("{}", init().await);
}

async fn init() -> u8 {
    1
}

The error is as follows:

error[E0728]: `await` is only allowed inside `async` functions and blocks
 --> src/main.rs:8:20
  |
7 | fn main() {
  |    ---- this is not `async`
8 |     println!("{}", init().await);
  |                    ^^^^^^^^^^^^ only allowed inside `async` functions and blocks

How to fix this error without using a third-party thread pool?

my Rust version is rustc 1.45.2 (d3fb005a3 2020-07-31)

You don't need a thread pool, but you do need an executor. It might be single-threaded, no problem.

And if you don't want to use an external one, well, just write your own! If you're really interested, check this material - the whole blog is focused on OS building, but this exact text might be helpful alone.

2 Likes

The Tokio tutorial has a section on building your own executor.

There is no executor in the standard library.

1 Like

Thank you very much

Thank you very much for your answer, I learned a lot.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.