]> git.cworth.org Git - rust-learning/commitdiff
Return an actual value from main
authorCarl Worth <cworth@cworth.org>
Tue, 10 Mar 2026 01:52:58 +0000 (18:52 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 12 Mar 2026 04:14:17 +0000 (21:14 -0700)
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.

src/main.rs

index 34e221406f5945a61d91f978893b8cd04e5df073..404dde8a9d07cbb061065322b0375781bea439db 100644 (file)
@@ -1,7 +1,10 @@
-fn main() {
+use std::error::Error;
+
+fn main() -> Result<(), Box<dyn Error>> {
     let args: Vec<String> = std::env::args().collect();
     if args.len() < 2 {
-        eprintln!("Usage: {} <race-config.json>", args[0]);
-        std::process::exit(1);
+        return Err(format!("Usage: {} <race-config.json>", args[0]).into());
     }
+
+    Ok(())
 }