]> git.cworth.org Git - rust-learning/commitdiff
Switch from expect to map_err. main
authorCarl Worth <cworth@cworth.org>
Tue, 10 Mar 2026 15:08:01 +0000 (08:08 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 12 Mar 2026 04:14:58 +0000 (21:14 -0700)
This lets us make a clean exit rather than triggering a panic, (which
has some extra noise and suggests something more exceptional than we
really want here).

Note our message is reworded slightly since map_err doesn't give us
full control of the string. It emits:

Error: "<our-string>"

so we don't want to start our string with "Error" as that would look
redundant.

I don't love not having full control of the error message, but still,
I think it is a reasonable compromise given how clean the code is with
.map_err.

src/main.rs

index 657e481d3386c11e67883350707c33d31cf22ccf..7123ed31a6341da8af0ca8288bf13354d4e892d7 100644 (file)
@@ -15,7 +15,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
 
     let config_path = std::path::Path::new(&args[1]);
     let config = config::load_config(config_path)
 
     let config_path = std::path::Path::new(&args[1]);
     let config = config::load_config(config_path)
-        .expect(format!("Error loading config file {}", args[1]).as_str());
+        .map_err(|e| format!("Failed to open config file {}: {}", args[1], e))?;
 
     eprintln!("Race: {}", config.name);
 
 
     eprintln!("Race: {}", config.name);