Rust handles errors using the Result enum for recoverable errors and the panic! macro for unrecoverable ones, forcing you to explicitly handle failures instead of relying on exceptions. Use match or the ? operator to propagate or handle Result values, ensuring your code compiles only when all error paths are addressed.
use std::fs::File;
fn read_file() -> Result<String, std::io::Error> {
let file = File::open("hello.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}