Use std::fs::read_to_string for the simplest approach, or BufReader with read_to_string for large files to avoid unnecessary memory allocations. Both methods return a Result<String, Error> that you must handle, typically using ? in functions returning Result or match/unwrap in main.
For most standard use cases, read_to_string is sufficient and idiomatic:
use std::fs;
use std::io;
fn read_config(path: &str) -> io::Result<String> {
// Reads the entire file into a String in one call
fs::read_to_string(path)
}
fn main() -> io::Result<()> {
let contents = read_config("config.txt")?;
println!("File length: {} bytes", contents.len());
Ok(())
}
If you are dealing with very large files where you want to minimize memory pressure or need to read in chunks before converting to a string, use std::io::BufReader. This buffers the input, making the read operation more efficient than reading byte-by-byte, though read_to_string on a File already handles buffering internally for the final string allocation. The BufReader approach is primarily useful if you need to inspect the stream before committing to a full string allocation or if you are chaining multiple read operations.
use std::fs::File;
use std::io::{self, BufReader, Read};
fn read_large_file(path: &str) -> io::Result<String> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let mut contents = String::new();
// Reads until EOF, appending to the string
reader.read_to_string(&mut contents)?;
Ok(contents)
}
Key considerations:
- Error Handling: Always handle the
Result. Inmain, returningio::Result<()>allows you to use the?operator to propagate errors immediately. - Encoding: These methods assume UTF-8. If the file contains invalid UTF-8 sequences,
read_to_stringwill return an error. For binary data or non-UTF-8 text, read into aVec<u8>usingfs::readinstead. - Performance:
read_to_stringpre-allocates memory based on the file size if known, but for massive files, streaming processing (reading line-by-line or chunk-by-chunk) is often more memory-efficient than loading the entire content into a singleString.