std::fs and std::io for file operations.
Read entire file:
1let content = std::fs::read_to_string("file.txt")?;
Write file:
1std::fs::write("output.txt", "Hello!")?;
Buffered read:
1use std::io::{BufRead, BufReader};2use std::fs::File;34let file = File::open("file.txt")?;5let reader = BufReader::new(file);6for line in reader.lines() {7 println!("{}", line?);8}
Async file I/O:
1use tokio::io::{AsyncReadExt, AsyncWriteExt};23let mut file = tokio::fs::File::open("file.txt").await?;4let mut contents = String::new();5file.read_to_string(&mut contents).await?;
Key: fs for sync, tokio::fs for async.