Skip to content

Instantly share code, notes, and snippets.

@gmodena
Last active October 25, 2019 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmodena/d9ab4b23f66f669aa59a2dd4ecef0e2e to your computer and use it in GitHub Desktop.
Save gmodena/d9ab4b23f66f669aa59a2dd4ecef0e2e to your computer and use it in GitHub Desktop.
/// 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