Rust 的标准库是否支持直接 IO 文件访问?

Does Rust's standard library support direct IO file access?

有没有办法用 Rust 的标准库指定 O_DIRECT,还是需要使用 libc

您可以使用 Unix 特定的 扩展特性 os::unix::fs::OpenOptionsExt:

use std::{fs::OpenOptions, os::unix::fs::OpenOptionsExt};

const O_DIRECT: i32 = 0o0040000; // Double check value

fn main() {
    OpenOptions::new()
        .read(true)
        .custom_flags(O_DIRECT)
        .open("/etc/passwd")
        .expect("Can't open");
}

但是,O_DIRECT 的值是特定于平台的。我可能最终会使用 libc 来提供价值。