rust - 如何异步读取文件?

标签 rust

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

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

最佳答案

使用 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);
}

关于rust - 如何异步读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360699/

相关文章:

rust - 链接迭代器以引用不同的生活时间

macros - 无法使用 rsfuzzy crate 中的宏

string - Rust 编程竞赛中最快的惯用 I/O 例程?

rust - 如何在 Rust 中同时迭代三个可迭代对象

rust - 有没有一种优雅的方法来重写使用 `match`语句获取或创建Option? [复制]

mysql - 如何将 Homebrew 安装的 mysql-client 与 diesel-cli 链接起来?

rust - 即使src/main.rs可用, cargo 也不会生成

python - Rust:逐个字符打印出字符串

casting - 如何将字节数组转换为 Rust 中的基本类型?

rust - Rust 如何知道变量是否可变?