use std::error::Error;
use std::fs;
fn read_first_line(path: &str) -> Result<String, Box<dyn Error>> {
let contents = fs::read_to_string(path)?; // io::Error → Box<dyn Error>
let line = contents.lines().next()
.ok_or("file was empty")?; // &str → Box<dyn Error>
Ok(line.to_string())
}
fn main() -> Result<(), Box<dyn Error>> {
let first = read_first_line("Cargo.toml")?;
println!("first line: {first}");
Ok(())
}
Create a free account and build your private vault. Share publicly whenever you want.