From: Carl Worth Date: Tue, 10 Mar 2026 01:52:58 +0000 (-0700) Subject: Return an actual value from main X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=45b4fb4058591995c405572f8921086b79325077;p=rust-learning Return an actual value from main This is a more idiomatic behavior from main, using a Result to return either the unit type (which will cause a return of 0) or else an Error (coerced from a string by using .into()). In that case, the program will print "Error: " and our error string before exiting with a value of 1. --- diff --git a/src/main.rs b/src/main.rs index 34e2214..404dde8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ -fn main() { +use std::error::Error; + +fn main() -> Result<(), Box> { let args: Vec = std::env::args().collect(); if args.len() < 2 { - eprintln!("Usage: {} ", args[0]); - std::process::exit(1); + return Err(format!("Usage: {} ", args[0]).into()); } + + Ok(()) }