]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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. | |
1a4d82fc | 10 | // |
223e47cc | 11 | |
223e47cc LB |
12 | |
13 | trait to_str { | |
1a4d82fc | 14 | fn to_string_(&self) -> String; |
223e47cc LB |
15 | } |
16 | ||
c34b1796 | 17 | impl to_str for isize { |
1a4d82fc | 18 | fn to_string_(&self) -> String { self.to_string() } |
223e47cc LB |
19 | } |
20 | ||
1a4d82fc JJ |
21 | impl<T:to_str> to_str for Vec<T> { |
22 | fn to_string_(&self) -> String { | |
23 | format!("[{}]", | |
24 | self.iter() | |
25 | .map(|e| e.to_string_()) | |
26 | .collect::<Vec<String>>() | |
c1a9b12d | 27 | .join(", ")) |
223e47cc LB |
28 | } |
29 | } | |
30 | ||
31 | pub fn main() { | |
62682a34 | 32 | assert_eq!(1.to_string_(), "1".to_string()); |
c30ab7b3 | 33 | assert_eq!((vec![2, 3, 4]).to_string_(), "[2, 3, 4]".to_string()); |
223e47cc | 34 | |
1a4d82fc JJ |
35 | fn indirect<T:to_str>(x: T) -> String { |
36 | format!("{}!", x.to_string_()) | |
223e47cc | 37 | } |
c30ab7b3 | 38 | assert_eq!(indirect(vec![10, 20]), "[10, 20]!".to_string()); |
223e47cc | 39 | |
1a4d82fc | 40 | fn indirect2<T:to_str>(x: T) -> String { |
223e47cc LB |
41 | indirect(x) |
42 | } | |
c30ab7b3 | 43 | assert_eq!(indirect2(vec![1]), "[1]!".to_string()); |
223e47cc | 44 | } |