如何异步读取文件?

Qua*_*rew 5 rust

我可以创建一个单独的线程来充当I/O队列,但我不确定这是否是最好的方法.它看起来是最好的.

我不知道如何用mio加载本地文件.

xx1*_*1xx 4

使用 tokio::fs::read:

use tokio::prelude::Future;

fn main() {
    let task = tokio::fs::read("/proc/cpuinfo").map(|data| {
        // do something with the contents of the file ...
        println!("contains {} bytes", data.len());
        println!("{:?}", String::from_utf8(data));
    }).map_err(|e| {
        // handle errors
        eprintln!("IO error: {:?}", e);
    });
    tokio::run(task);
}
Run Code Online (Sandbox Code Playgroud)