1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 fn say(&self, s:&str) -> String;
14 fn hi(&self) -> String { hello(self) }
17 fn hello<S:Speak>(s:&S) -> String{
21 impl Speak for isize {
22 fn say(&self, s:&str) -> String {
23 format!("{}: {}", s, *self)
27 impl<T: Speak> Speak for Option<T> {
28 fn say(&self, s:&str) -> String {
30 None => format!("{} - none", s),
31 Some(ref x) => { format!("something!{}", x.say(s)) }
38 assert_eq!(3.hi(), "hello: 3".to_string());
39 assert_eq!(Some(Some(3)).hi(),
40 "something!something!hello: 3".to_string());
41 assert_eq!(None::<isize>.hi(), "hello - none".to_string());
43 assert_eq!(Some(None::<isize>).hi(), "something!hello - none".to_string());
44 assert_eq!(Some(3).hi(), "something!hello: 3".to_string());