2013-06-28 19:47:44 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
|
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
|
|
|
}
|