Last active
October 25, 2019 21:45
-
-
Save gmodena/d9ab4b23f66f669aa59a2dd4ecef0e2e 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
/// String parsing example | |
impl FromStr for InputRange { | |
type Err = ParseError; | |
fn from_str(input: &str) -> std::result::Result<Self, Self::Err> { | |
lazy_static! { | |
static ref RE: Regex = Regex::new(r"(\d+)-(\d+)").unwrap(); | |
}; | |
let parse_error_msg= "Could not parse input range"; | |
let captures = RE.captures(input).unwrap(); | |
let low = captures.get(1).map_or("", |m| m.as_str()) | |
.parse::<u32>() | |
.expect(parse_error_msg); | |
let high = captures.get(2).map_or("", |m| m.as_str()) | |
.parse::<u32>() | |
.expect(parse_error_msg); | |
Ok(InputRange { low, high }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment