Files

1

Files, Directories, and Processes

5 minute

C tools and libraries quickly reach for interfaces like these:

FILE *fp = fopen(path, "rb");
fread(buf, 1, sizeof(buf), fp);
fclose(fp);

DIR *dir = opendir(path);
system("gzip file.log");
getenv("HOME");

Rust’s standard library splits the same area into a few modules:

std::fs       -> convenient file and directory operations
std::io       -> Read / Write / BufRead traits
std::env      -> arguments, environment variables, current directory
std::process  -> child processes, exit status, captured output

This article focuses on the first replacement layer. Ownership and automatic cleanup are covered by the Drop article.

Read More