Applied Intermediate
13 Combinators
Most match statements on Option have a one-word replacement. Transform the inside of an Option with map, then fold a whole parse-with-default flow into a single chain.
In port, parse the configured port or fall back to 8080 -- one chain, no branches.
Hints
and_thenchains a step that itself returns anOption;parse().ok()is exactly that.- Finish with
.unwrap_or(8080).
Reveal solution
fn port(config: Option<&str>) -> u16 {
config.and_then(|s| s.parse().ok()).unwrap_or(8080)
}