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.
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.
13 The `ToStr` trait for converting to strings
17 use option::{Some, None};
24 use vec::ImmutableVector;
26 /// A generic trait for converting a value to a string
28 /// Converts the value of `self` to an owned string
29 fn to_str(&self) -> ~str;
32 /// Trait for converting a type to a string, consuming it in the process.
34 /// Consume and convert to a string.
35 fn into_str(self) -> ~str;
40 fn to_str(&self) -> ~str { ~"()" }
43 impl<A:ToStr+Hash+Eq, B:ToStr> ToStr for HashMap<A, B> {
45 fn to_str(&self) -> ~str {
48 for (key, value) in self.iter() {
55 acc.push_str(key.to_str());
57 acc.push_str(value.to_str());
64 impl<A:ToStr+Hash+Eq> ToStr for HashSet<A> {
66 fn to_str(&self) -> ~str {
69 for element in self.iter() {
76 acc.push_str(element.to_str());
83 impl<'a,A:ToStr> ToStr for &'a [A] {
85 fn to_str(&self) -> ~str {
88 for elt in self.iter() {
95 acc.push_str(elt.to_str());
102 impl<A:ToStr> ToStr for ~[A] {
104 fn to_str(&self) -> ~str {
106 let mut first = true;
107 for elt in self.iter() {
114 acc.push_str(elt.to_str());
123 use hashmap::HashMap;
124 use hashmap::HashSet;
125 use container::{MutableSet, MutableMap};
129 fn test_simple_types() {
130 assert_eq!(1i.to_str(), ~"1");
131 assert_eq!((-1i).to_str(), ~"-1");
132 assert_eq!(200u.to_str(), ~"200");
133 assert_eq!(2u8.to_str(), ~"2");
134 assert_eq!(true.to_str(), ~"true");
135 assert_eq!(false.to_str(), ~"false");
136 assert_eq!(().to_str(), ~"()");
137 assert_eq!((~"hi").to_str(), ~"hi");
143 assert_eq!(x.to_str(), ~"[]");
144 assert_eq!((~[1]).to_str(), ~"[1]");
145 assert_eq!((~[1, 2, 3]).to_str(), ~"[1, 2, 3]");
146 assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
147 ~"[[], [1], [1, 1]]");
150 struct StructWithToStrWithoutEqOrHash {
154 impl ToStr for StructWithToStrWithoutEqOrHash {
155 fn to_str(&self) -> ~str {
156 format!("s{}", self.value)
162 let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
163 let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
165 table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 });
166 table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 });
168 let table_str = table.to_str();
170 assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
171 assert_eq!(empty.to_str(), ~"{}");
176 let mut set: HashSet<int> = HashSet::new();
177 let empty_set: HashSet<int> = HashSet::new();
182 let set_str = set.to_str();
184 assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
185 assert_eq!(empty_set.to_str(), ~"{}");