]> git.lizzy.rs Git - rust.git/blob - src/libstd/to_str.rs
Move std::{trie, hashmap} to libcollections
[rust.git] / src / libstd / to_str.rs
1 // Copyright 2012-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
11 /*!
12
13 The `ToStr` trait for converting to strings
14
15 */
16
17 use option::{Some, None};
18 use str::OwnedStr;
19 use iter::Iterator;
20 use vec::ImmutableVector;
21
22 /// A generic trait for converting a value to a string
23 pub trait ToStr {
24     /// Converts the value of `self` to an owned string
25     fn to_str(&self) -> ~str;
26 }
27
28 /// Trait for converting a type to a string, consuming it in the process.
29 pub trait IntoStr {
30     /// Consume and convert to a string.
31     fn into_str(self) -> ~str;
32 }
33
34 impl ToStr for () {
35     #[inline]
36     fn to_str(&self) -> ~str { ~"()" }
37 }
38
39 impl<'a,A:ToStr> ToStr for &'a [A] {
40     #[inline]
41     fn to_str(&self) -> ~str {
42         let mut acc = ~"[";
43         let mut first = true;
44         for elt in self.iter() {
45             if first {
46                 first = false;
47             }
48             else {
49                 acc.push_str(", ");
50             }
51             acc.push_str(elt.to_str());
52         }
53         acc.push_char(']');
54         acc
55     }
56 }
57
58 impl<A:ToStr> ToStr for ~[A] {
59     #[inline]
60     fn to_str(&self) -> ~str {
61         let mut acc = ~"[";
62         let mut first = true;
63         for elt in self.iter() {
64             if first {
65                 first = false;
66             }
67             else {
68                 acc.push_str(", ");
69             }
70             acc.push_str(elt.to_str());
71         }
72         acc.push_char(']');
73         acc
74     }
75 }
76
77 #[cfg(test)]
78 mod tests {
79     use super::*;
80
81     #[test]
82     fn test_simple_types() {
83         assert_eq!(1i.to_str(), ~"1");
84         assert_eq!((-1i).to_str(), ~"-1");
85         assert_eq!(200u.to_str(), ~"200");
86         assert_eq!(2u8.to_str(), ~"2");
87         assert_eq!(true.to_str(), ~"true");
88         assert_eq!(false.to_str(), ~"false");
89         assert_eq!(().to_str(), ~"()");
90         assert_eq!((~"hi").to_str(), ~"hi");
91     }
92
93     #[test]
94     fn test_vectors() {
95         let x: ~[int] = ~[];
96         assert_eq!(x.to_str(), ~"[]");
97         assert_eq!((~[1]).to_str(), ~"[1]");
98         assert_eq!((~[1, 2, 3]).to_str(), ~"[1, 2, 3]");
99         assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
100                ~"[[], [1], [1, 1]]");
101     }
102 }