Skip to content

Instantly share code, notes, and snippets.

@msullivan
Created June 20, 2013 03:36
Show Gist options
  • Select an option

  • Save msullivan/5820128 to your computer and use it in GitHub Desktop.

Select an option

Save msullivan/5820128 to your computer and use it in GitHub Desktop.
#[allow(default_methods)]
trait Speak {
fn say(&self, s:&str);
fn hi(&self) { hello(self); }
}
fn hello<S:Speak>(s:&S) {
s.say("hello");
}
impl Speak for int {
fn say(&self, s:&str) {
println(fmt!("%s: %d", s, *self));
}
}
impl<T: Speak> Speak for Option<T> {
fn say(&self, s:&str) {
match *self {
None => println(fmt!("%s to nobody", s)),
Some(ref x) => { println("something!"); x.say(s); }
}
}
}
fn main() {
3.hi();
Some(3).hi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment