Created
August 6, 2018 07:21
-
-
Save shakyShane/ac493cf66fb5877f81336b6604d7390d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn modify_url(caps: &Captures, host: &str, port: u16) -> Option<String> { | |
let first_match = caps.iter().nth(0)?; | |
let match_item = first_match?; | |
if let Ok(mut url) = Url::parse(match_item.as_str()) { | |
// The following 2 method calls will mutate | |
// the url, but return *different* Errors if they fail | |
// | |
// Because I don't care about the actual error, only | |
// if it succeeded or failed, you can convert a Result | |
// to Option via .ok() | |
// | |
// So if setting the host or port fails here, just do | |
// nothing and the `?` will short-circuit and return a None | |
// for this function | |
url.set_host(Some(host)).ok()?; | |
url.set_port(Some(port)).ok()?; | |
Some(url.to_string()) | |
} else { | |
None | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment