Attribute Macro tokio_macros::main_rt
source · [−]#[main_rt]
Expand description
Marks async function to be executed by selected runtime. This macro helps set up a Runtime
without requiring the user to use Runtime or
Builder directly.
Function arguments:
Arguments are allowed for any functions aside from main
which is special
Usage
Using default
#[tokio::main(flavor = "current_thread")]
async fn main() {
println!("Hello world");
}
Equivalent code not using #[tokio::main]
fn main() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
println!("Hello world");
})
}
NOTE:
If you rename the Tokio crate in your dependencies this macro will not work.
If you must rename the current version of Tokio because you’re also using an
older version of Tokio, you must make the current version of Tokio
available as tokio
in the module where this macro is expanded.