]>
Commit | Line | Data |
---|---|---|
970d7e83 LB |
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. | |
4 | // | |
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. | |
10 | ||
c34b1796 | 11 | |
1a4d82fc JJ |
12 | trait Speak : Sized { |
13 | fn say(&self, s:&str) -> String; | |
14 | fn hi(&self) -> String { hello(self) } | |
970d7e83 LB |
15 | } |
16 | ||
1a4d82fc | 17 | fn hello<S:Speak>(s:&S) -> String{ |
970d7e83 LB |
18 | s.say("hello") |
19 | } | |
20 | ||
c34b1796 | 21 | impl Speak for isize { |
1a4d82fc JJ |
22 | fn say(&self, s:&str) -> String { |
23 | format!("{}: {}", s, *self) | |
970d7e83 LB |
24 | } |
25 | } | |
26 | ||
27 | impl<T: Speak> Speak for Option<T> { | |
1a4d82fc | 28 | fn say(&self, s:&str) -> String { |
970d7e83 | 29 | match *self { |
1a4d82fc JJ |
30 | None => format!("{} - none", s), |
31 | Some(ref x) => { format!("something!{}", x.say(s)) } | |
970d7e83 LB |
32 | } |
33 | } | |
34 | } | |
35 | ||
36 | ||
1a4d82fc | 37 | pub fn main() { |
85aaf69f SL |
38 | assert_eq!(3.hi(), "hello: 3".to_string()); |
39 | assert_eq!(Some(Some(3)).hi(), | |
1a4d82fc | 40 | "something!something!hello: 3".to_string()); |
c34b1796 | 41 | assert_eq!(None::<isize>.hi(), "hello - none".to_string()); |
970d7e83 | 42 | |
c34b1796 | 43 | assert_eq!(Some(None::<isize>).hi(), "something!hello - none".to_string()); |
85aaf69f | 44 | assert_eq!(Some(3).hi(), "something!hello: 3".to_string()); |
970d7e83 | 45 | } |