Well, Result
's map_err
does not always return Ok
, or always return Err
. So I guess I don't quite see the connection.
Viewing Option<T>
as something that's almost the same as Result<T, ()>
, I'd come to the same conclusion as @hyeonu above, that a an Option::map_none
should, if it exists, call a FnOnce() -> ()
callback, or possibly FnOnce(()) -> ()
, which doesn't really do anything of transforming the Option
value though. Just executes some potential side-effects in the None
case; which could probably be more clearly achieved with an if foo.is_none() { /* do something *}; /* keep using `foo` */
I'll admit, it's an impressive amount of closing parentheses in the end.
I feel like rustfmt does an okay job to keep it readable. E.g.
impl<'a> HttpResponse<'a> {
pub fn new(headers: Option<HashMap<&'a str, &'a str>>) -> Self {
HttpResponse {
headers: Some(headers.unwrap_or_else(|| {
HashMap::from_iter([
("xx", "zz"),
("yy", "aa"),
("zz", "bb"),
("aa", "f"),
("foo", "bar"),
])
})),
}
}
}
Alternatively, some indentation and parentheses can be saved by using a local variable, e.g.
impl<'a> HttpResponse<'a> {
pub fn new(headers: Option<HashMap<&'a str, &'a str>>) -> Self {
let headers = headers.unwrap_or_else(|| {
HashMap::from_iter([
("xx", "zz"),
("yy", "aa"),
("zz", "bb"),
("aa", "f"),
("foo", "bar"),
])
});
HttpResponse {
headers: Some(headers),
}
}
}
well, actually that saves less indentation than I thought it would... anyways, the use of from_iter
was just an unrelated suggestion that you can follow if you want to, but don't have to.