文件、目录和进程
C 程序员写工具或基础库,很快会碰到这些接口:
FILE *fp = fopen(path, "rb");
fread(buf, 1, sizeof(buf), fp);
fclose(fp);
DIR *dir = opendir(path);
system("gzip file.log");
getenv("HOME");
Rust 标准库里对应的入口主要是:
std::fs -> 文件和目录的便捷操作
std::io -> Read / Write / BufRead 等 I/O trait
std::env -> 参数、环境变量、当前目录
std::process -> 启动子进程、取退出状态和输出
这一篇不展开所有权和 Drop 的细节,只看 C 常用库函数在 Rust 里的第一层替代方式。
阅读更多