Skip to content

Instantly share code, notes, and snippets.

@brunosaboia
Created September 6, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brunosaboia/d861e1a190652c85511d936282a4eea6 to your computer and use it in GitHub Desktop.
Save brunosaboia/d861e1a190652c85511d936282a4eea6 to your computer and use it in GitHub Desktop.
use std::str::FromStr;
struct ParseError<'a, E> {
error: E,
source: &'a str,
}
fn parse<'a, T: FromStr>(s: &'a str) -> Result<T, ParseError<'a, T::Err>> {
s.parse().map_err(|e| ParseError { error: e, source: s })
}
fn main() {
let a: Result<i32, _> = parse("asdf");
match a {
Err(e) => println!("Couldn't parse {:?}: {}", e.source, e.error),
Ok(i) => println!("Parsed: {}", i),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment