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