How to log errors properly in Rust

Use Result types for recoverable errors and panic for unrecoverable states to ensure safe Rust error handling.

Use Result<T, E> for recoverable errors and panic! only for unrecoverable states.

use std::fs::File;

fn read_file(path: &str) -> Result<String, std::io::Error> {
    let mut file = File::open(path)?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    Ok(contents)
}

fn main() {
    match read_file("data.txt") {
        Ok(content) => println!("File content: {content}"),
        Err(e) => eprintln!("Failed to read file: {e}"),
    }
}