2013-02-23 17:41:44 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -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.
|
|
|
|
|
2012-09-19 16:52:32 -07:00
|
|
|
/*!
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
The `ToString` trait for converting to strings
|
2012-09-19 16:52:32 -07:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-06-30 17:22:40 -07:00
|
|
|
#![experimental]
|
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
use fmt;
|
2014-05-22 16:57:53 -07:00
|
|
|
use string::String;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A generic trait for converting a value to a string
|
2014-06-21 03:39:03 -07:00
|
|
|
pub trait ToString {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Converts the value of `self` to an owned string
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string(&self) -> String;
|
2013-02-03 20:47:26 -08:00
|
|
|
}
|
2012-02-22 13:06:38 +01:00
|
|
|
|
2013-04-20 19:39:15 +02:00
|
|
|
/// Trait for converting a type to a string, consuming it in the process.
|
2013-12-15 01:04:22 +11:00
|
|
|
pub trait IntoStr {
|
2013-08-18 08:28:04 +10:00
|
|
|
/// Consume and convert to a string.
|
2014-06-21 03:39:03 -07:00
|
|
|
fn into_string(self) -> String;
|
2013-04-20 19:39:15 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
impl<T: fmt::Show> ToString for T {
|
|
|
|
fn to_string(&self) -> String {
|
2014-05-27 20:44:58 -07:00
|
|
|
format!("{}", *self)
|
2014-05-16 10:45:16 -07:00
|
|
|
}
|
2013-02-27 16:01:53 +01:00
|
|
|
}
|
|
|
|
|
2012-02-22 13:06:38 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-06-06 10:27:49 -07:00
|
|
|
use prelude::*;
|
2013-07-23 16:40:25 +04:00
|
|
|
use super::*;
|
|
|
|
|
2012-02-22 13:06:38 +01:00
|
|
|
#[test]
|
|
|
|
fn test_simple_types() {
|
2014-06-21 03:39:03 -07:00
|
|
|
assert_eq!(1i.to_string(), "1".to_string());
|
|
|
|
assert_eq!((-1i).to_string(), "-1".to_string());
|
|
|
|
assert_eq!(200u.to_string(), "200".to_string());
|
|
|
|
assert_eq!(2u8.to_string(), "2".to_string());
|
|
|
|
assert_eq!(true.to_string(), "true".to_string());
|
|
|
|
assert_eq!(false.to_string(), "false".to_string());
|
|
|
|
assert_eq!(().to_string(), "()".to_string());
|
|
|
|
assert_eq!(("hi".to_string()).to_string(), "hi".to_string());
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
|
|
|
|
2012-07-08 00:39:20 -07:00
|
|
|
#[test]
|
2012-02-22 13:06:38 +01:00
|
|
|
fn test_vectors() {
|
2014-06-06 10:27:49 -07:00
|
|
|
let x: Vec<int> = vec![];
|
2014-06-21 03:39:03 -07:00
|
|
|
assert_eq!(x.to_string(), "[]".to_string());
|
|
|
|
assert_eq!((vec![1i]).to_string(), "[1]".to_string());
|
|
|
|
assert_eq!((vec![1i, 2, 3]).to_string(), "[1, 2, 3]".to_string());
|
|
|
|
assert!((vec![vec![], vec![1i], vec![1i, 1]]).to_string() ==
|
2014-05-25 03:17:19 -07:00
|
|
|
"[[], [1], [1, 1]]".to_string());
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
2013-05-12 20:34:15 -04:00
|
|
|
}
|