]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/trait-to-str.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / test / run-pass / trait-to-str.rs
CommitLineData
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
13trait to_str {
1a4d82fc 14 fn to_string_(&self) -> String;
223e47cc
LB
15}
16
c34b1796 17impl to_str for isize {
1a4d82fc 18 fn to_string_(&self) -> String { self.to_string() }
223e47cc
LB
19}
20
1a4d82fc
JJ
21impl<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>>()
27 .connect(", "))
223e47cc
LB
28 }
29}
30
31pub fn main() {
1a4d82fc 32 assert!(1.to_string_() == "1".to_string());
85aaf69f 33 assert!((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 }
85aaf69f 38 assert!(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 }
85aaf69f 43 assert!(indirect2(vec!(1)) == "[1]!".to_string());
223e47cc 44}