I have written some code that parses a config file. If the config file holds a valid value for a field it sets in Config
struct. If no valid integer value was found for a setting it sets a default value (e.g: 90).
let config = Config {
interval: settings.get("interval").unwrap().parse().unwrap_or(90),
}
How can I make this to take a closure, so that it can print via error!
and set default value?
Looking something like following:
let config = Config {
interval: settings.get("interval").unwrap().parse().unwrap_or({
error!("No interval found. Using default: 90");
90
});
}
But in this example, the error!
is always executed, even if a valid value from interval was read from the config.
How can I make it so that unwrap_or only executes the code in optb
when parse()
has failed?