]> git.cworth.org Git - rust-learning/commitdiff
Use expect instead of unwrap_or_else
authorCarl Worth <cworth@cworth.org>
Tue, 10 Mar 2026 14:56:54 +0000 (07:56 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 12 Mar 2026 04:14:58 +0000 (21:14 -0700)
I just learned about this convenient method provided by Result.
Compared to using unwrap_or_else, I don't need to explicitly call
std::process::exit() and compared to using ? I still get to control
the error string (to include the filename in it).

src/main.rs

index ecb0dcda36e88a60fdcecf96275e69b9c967fead..657e481d3386c11e67883350707c33d31cf22ccf 100644 (file)
@@ -14,10 +14,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
     }
 
     let config_path = std::path::Path::new(&args[1]);
-    let config = config::load_config(config_path).unwrap_or_else(|e| {
-        eprintln!("Error loading config file {}: {}", args[1], e);
-        std::process::exit(1);
-    });
+    let config = config::load_config(config_path)
+        .expect(format!("Error loading config file {}", args[1]).as_str());
 
     eprintln!("Race: {}", config.name);