fs
1. Writing to a File
// let content = "yashaswa is the best coder";
// let path = "output.txt";
// fs::write(path, content)fs::writewrites text to a file.- If the file does not exist, it creates one.
- If the file exists, it overwrites it.
- Always handle errors with
matchto avoid silent failures.
2. Reading from a File
// let path = "output.txt";
// fs::read_to_string(path)fs::read_to_stringreads a file’s full content into aString.- Works best for small to medium text files.
- Always check for errors (e.g., missing file or permission issue).
3. Directory Operations
let dir_path = "my_app_data";
fs::create_dir_all(dir_path)create_dir_allmakes a directory.- If it already exists, no error is thrown.
- Important to check for errors; if directory creation fails, stop further work.
Writing Data Inside the Directory
let file_path = "my_app_data/data.txt";
fs::write(file_path, "some important data")- Creates a new file inside the directory.
- Saves
"some important data"into it.
Listing Directory Contents
fs::read_dir(dir_path)- Reads entries inside the directory.
- Must handle each entry carefully, since errors can happen while reading.
entry.path().display()shows the file or folder path in a readable format.
Key Takeaways
- Always check results with
matchorif letto avoid crashes. - Writing overwrites files; be careful not to lose old data.
- Directories may already exist, so handle that case smoothly.
- Reading directories is useful to confirm what files you have created.