// Created on savesnippets.com ยท https://savesnippets.com/2Z6Gf087COHRqg // Cargo.toml: anyhow = "1" use anyhow::{Context, Result, bail}; fn run() -> Result<()> { let config = std::fs::read_to_string("config.toml") .context("reading config.toml")?; // adds breadcrumb if it fails let port: u16 = config.trim().parse() .with_context(|| format!("parsing port from {config:?}"))?; if port == 0 { bail!("port cannot be zero"); // shorthand for return Err(anyhow!()) } println!("listening on :{port}"); Ok(()) } fn main() { if let Err(e) = run() { eprintln!("error: {e:#}"); // {:#} prints full causal chain std::process::exit(1); } }