Created
December 21, 2014 03:51
-
-
Save rustyrazorblade/10f13c932923922acd98 to your computer and use it in GitHub Desktop.
traits - adding to an existing type
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
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