I'm learning Rust and I am struggling with optional URL parsing, I've got the following code:
use hyper::Uri; fn main() -> Result<(), String> { let url = Some("http://www.stackoverflow.com"); let works = if let Some(url) = url { Some(parse_url(url)?) } else { None }; let does_not = url.map(|u| parse_url(u)?); Ok(()) } fn parse_url(url_str: &str) -> Result<Uri, String> { unimplemented!(); } Documentation says that calling ? can be used as a syntactic sugar to simplify error handling.
So what bothers me is that I can perfectly fine map the inner value of Option<T> manually, using if let syntax and return early from the method but cannot just .map() the inner value of Option<T>.
So my question is actually how can I easily parse an optional value by avoiding if/else conditions in Rust?