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.
|
|
|
|
|
|
|
|
trait Speak {
|
|
|
|
fn say(&self, s:&str) -> ~str;
|
|
|
|
fn hi(&self) -> ~str { hello(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hello<S:Speak>(s:&S) -> ~str{
|
|
|
|
s.say("hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Speak for int {
|
|
|
|
fn say(&self, s:&str) -> ~str {
|
2013-09-29 21:23:57 -05:00
|
|
|
format!("{}: {}", s, *self)
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Speak> Speak for Option<T> {
|
|
|
|
fn say(&self, s:&str) -> ~str {
|
|
|
|
match *self {
|
2013-09-29 21:23:57 -05:00
|
|
|
None => format!("{} - none", s),
|
2014-04-15 20:17:48 -05:00
|
|
|
Some(ref x) => { "something!".to_owned() + x.say(s) }
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-25 02:43:37 -05:00
|
|
|
pub fn main() {
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(3.hi(), "hello: 3".to_owned());
|
|
|
|
assert_eq!(Some(Some(3)).hi(), "something!something!hello: 3".to_owned());
|
|
|
|
assert_eq!(None::<int>.hi(), "hello - none".to_owned());
|
2013-06-28 19:47:44 -05:00
|
|
|
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(Some(None::<int>).hi(), "something!hello - none".to_owned());
|
|
|
|
assert_eq!(Some(3).hi(), "something!hello: 3".to_owned());
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|