How to use unwrap_or and unwrap_or_else

Use unwrap_or for static defaults and unwrap_or_else for dynamic error handling like logging or exiting.

Use unwrap_or to provide a static default value if a Result or Option fails, and unwrap_or_else to execute a closure for dynamic error handling or cleanup. Use unwrap_or when the fallback is cheap and constant, like a default number. Use unwrap_or_else when you need to log an error, exit the process, or compute a value only if the failure occurs.

use std::process;

// Static default: returns 0 if the key is missing
let score = scores.get(&team_name).copied().unwrap_or(0);

// Dynamic handling: logs error and exits if config build fails
let config = Config::build(&args).unwrap_or_else(|err| {
    println!("Problem parsing arguments: {err}");
    process::exit(1);
});