2014-02-07 20:08:32 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 17:32:48 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2014-07-21 15:57:14 -07:00
|
|
|
//
|
2012-08-13 15:50:29 -07:00
|
|
|
|
2014-03-05 15:28:08 -08:00
|
|
|
|
2012-07-31 10:27:51 -07:00
|
|
|
trait to_str {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String;
|
2012-01-03 16:07:26 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
impl to_str for isize {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String { self.to_string() }
|
2012-01-03 16:07:26 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 14:02:44 -08:00
|
|
|
impl<T:to_str> to_str for Vec<T> {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String {
|
2014-05-27 20:44:58 -07:00
|
|
|
format!("[{}]",
|
|
|
|
self.iter()
|
2014-06-21 03:39:03 -07:00
|
|
|
.map(|e| e.to_string_())
|
2014-05-27 20:44:58 -07:00
|
|
|
.collect::<Vec<String>>()
|
2015-07-10 08:19:21 -04:00
|
|
|
.join(", "))
|
2012-01-03 16:07:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2015-06-07 21:00:38 +03:00
|
|
|
assert_eq!(1.to_string_(), "1".to_string());
|
|
|
|
assert_eq!((vec!(2, 3, 4)).to_string_(), "[2, 3, 4]".to_string());
|
2012-01-03 16:37:41 +01:00
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn indirect<T:to_str>(x: T) -> String {
|
2014-06-21 03:39:03 -07:00
|
|
|
format!("{}!", x.to_string_())
|
2012-01-03 16:07:26 +01:00
|
|
|
}
|
2015-06-07 21:00:38 +03:00
|
|
|
assert_eq!(indirect(vec!(10, 20)), "[10, 20]!".to_string());
|
2012-01-03 16:37:41 +01:00
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn indirect2<T:to_str>(x: T) -> String {
|
2013-02-15 02:44:18 -08:00
|
|
|
indirect(x)
|
2012-01-03 16:37:41 +01:00
|
|
|
}
|
2015-06-07 21:00:38 +03:00
|
|
|
assert_eq!(indirect2(vec!(1)), "[1]!".to_string());
|
2012-01-03 16:07:26 +01:00
|
|
|
}
|