]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-to-str.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / trait-to-str.rs
1 // Copyright 2012-2014 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 //
11 // ignore-lexer-test FIXME #15883
12
13
14 trait to_str {
15     fn to_string_(&self) -> String;
16 }
17
18 impl to_str for int {
19     fn to_string_(&self) -> String { self.to_string() }
20 }
21
22 impl<T:to_str> to_str for Vec<T> {
23     fn to_string_(&self) -> String {
24         format!("[{}]",
25                 self.iter()
26                     .map(|e| e.to_string_())
27                     .collect::<Vec<String>>()
28                     .connect(", "))
29     }
30 }
31
32 pub fn main() {
33     assert!(1.to_string_() == "1".to_string());
34     assert!((vec!(2i, 3, 4)).to_string_() == "[2, 3, 4]".to_string());
35
36     fn indirect<T:to_str>(x: T) -> String {
37         format!("{}!", x.to_string_())
38     }
39     assert!(indirect(vec!(10i, 20)) == "[10, 20]!".to_string());
40
41     fn indirect2<T:to_str>(x: T) -> String {
42         indirect(x)
43     }
44     assert!(indirect2(vec!(1i)) == "[1]!".to_string());
45 }