跳到主要内容

main_rt

搜索

Attribute Macro main_rt 

Source
#[main_rt]
展开描述

将 async 函数标记为由所选运行时执行。该宏帮助设置一个 Runtime, 无需用户直接使用 RuntimeBuilder

§函数参数:

main(特殊)外,允许任何函数使用参数

§用法

§Using default

#[tokio::main(flavor = "current_thread")]
async fn main() {
    println!("Hello world");
}

不使用 #[tokio::main] 的等价代码

fn main() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            println!("Hello world");
        })
}

§Rename package

use tokio as tokio1;

#[tokio1::main(crate = "tokio1")]
async fn main() {
    println!("Hello world");
}

不使用 #[tokio::main] 的等价代码

use tokio as tokio1;

fn main() {
    tokio1::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            println!("Hello world");
        })
}