QuickSnip
← Back to browse

Read a file to string

Idiomatic Rust file read with proper error propagation.

Rust14 linesUpdated Jul 17, 2026
Edit
Rust
use std::fs;
use std::io;

fn read_config(path: &str) -> io::Result<String> {
    let contents = fs::read_to_string(path)?;
    Ok(contents)
}

fn main() {
    match read_config("config.toml") {
        Ok(text) => println!("{text}"),
        Err(e) => eprintln!("error: {e}"),
    }
}