Skip to content

Main

Let’s make the main function a tokio entry point.

Add the #[tokio::main] macro to the main function and make the function async. This allows you to use async and await inside main. You can also now spawn tokio tasks within your application.

src/main.rs
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
println!("Sleeping for 5 seconds...");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
Ok(())
}

You can run this with cargo run, and you’ll see that the terminal prints and then blocks for 5 seconds before returning control.

Terminal window
$ cargo run
Compiling crates-tui v0.1.0 (~/gitrepos/crates-tui-tutorial)
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
Running `target/debug/crates-tui-tutorial`
Sleeping for 5 seconds...
$

Conclusion

We will expand on main.rs in the following sections. Right now, your project should look like this:

.
├── Cargo.lock
├── Cargo.toml
└── src
└── main.rs