Skip to content

Instantly share code, notes, and snippets.

@rustyrazorblade
Created December 21, 2014 03:51
Show Gist options
  • Save rustyrazorblade/10f13c932923922acd98 to your computer and use it in GitHub Desktop.
Save rustyrazorblade/10f13c932923922acd98 to your computer and use it in GitHub Desktop.
traits - adding to an existing type
trait ConvertToInt {
fn to_int(&self) -> Option<i64>;
}
impl ConvertToInt for String {
fn to_int(&self) -> Option<i64> {
let buf = self.as_slice();
let result: Option<i64> = from_str(buf);
return result
}
}
#[test]
fn test_convert_to_int() {
let a = "10".to_string().to_int().unwrap();
assert_eq!(10i64, a);
let a = "11".to_string().to_int().unwrap();
assert_eq!(11i64, a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment