Skip to content

Instantly share code, notes, and snippets.

@msullivan
Created June 26, 2012 16:25
Show Gist options
  • Save msullivan/2996840 to your computer and use it in GitHub Desktop.
Save msullivan/2996840 to your computer and use it in GitHub Desktop.
traits thing
#[warn(no_vecs_not_implicitly_copyable)];
iface to_str {
fn to_str() -> str;
}
impl of to_str for int {
fn to_str() -> str { int::to_str(self, 10u) }
}
impl of to_str for str {
fn to_str() -> str { self }
}
impl extensions<A: to_str> for A {
fn exclaim() -> str { self.to_str() + "!" }
}
fn exclaim<T: to_str>(x: T) {
io::println(x.exclaim());
}
fn main() {
exclaim(5);
exclaim("hello, world");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment