Rust 异步运行时原语

深入理解 Rust 的异步运行时

async-std

标准库的异步版本,提供与标准库兼容的异步API。

#[async_std::main]
async fn main() {
    let file = File::open("file.txt").await?;
    let content = read_to_string(file).await?;
}
中级 异步运行时

Tokio

高性能异步运行时,提供异步任务调度、IO操作和并发原语。

#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        // 异步任务
    });
    handle.await.unwrap();
}
中级 异步运行时

smol

小巧而强大的异步运行时,专注于简单性和可组合性。

smol::block_on(async {
    let task = smol::spawn(async {
        // 异步任务
    });
    task.await;
})
中级 异步运行时