2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
|
|
|
|
2014-12-19 11:54:09 +00:00
|
|
|
trait Speak : Sized {
|
2014-05-22 23:57:53 +00:00
|
|
|
fn say(&self, s:&str) -> String;
|
|
|
|
fn hi(&self) -> String { hello(self) }
|
2013-06-29 00:47:44 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
fn hello<S:Speak>(s:&S) -> String{
|
2013-06-29 00:47:44 +00:00
|
|
|
s.say("hello")
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
impl Speak for isize {
|
2014-05-22 23:57:53 +00:00
|
|
|
fn say(&self, s:&str) -> String {
|
2014-05-28 03:44:58 +00:00
|
|
|
format!("{}: {}", s, *self)
|
2013-06-29 00:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Speak> Speak for Option<T> {
|
2014-05-22 23:57:53 +00:00
|
|
|
fn say(&self, s:&str) -> String {
|
2013-06-29 00:47:44 +00:00
|
|
|
match *self {
|
2014-05-28 03:44:58 +00:00
|
|
|
None => format!("{} - none", s),
|
|
|
|
Some(ref x) => { format!("something!{}", x.say(s)) }
|
2013-06-29 00:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-25 07:43:37 +00:00
|
|
|
pub fn main() {
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(3.hi(), "hello: 3".to_string());
|
|
|
|
assert_eq!(Some(Some(3)).hi(),
|
2014-05-25 10:17:19 +00:00
|
|
|
"something!something!hello: 3".to_string());
|
2015-03-26 00:06:52 +00:00
|
|
|
assert_eq!(None::<isize>.hi(), "hello - none".to_string());
|
2013-06-29 00:47:44 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
assert_eq!(Some(None::<isize>).hi(), "something!hello - none".to_string());
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(Some(3).hi(), "something!hello: 3".to_string());
|
2013-06-29 00:47:44 +00:00
|
|
|
}
|