]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/to_str.rs
auto merge of #15421 : catharsis/rust/doc-ffi-minor-fixes, r=alexcrichton
[rust.git] / src / libstd / to_str.rs
index 4132c8a5b5a3332c80d3ace6a07d834b7f8e0b96..c19fd81b5705667185f8a8a9391fca11a9114dba 100644 (file)
 
 /*!
 
-The `ToStr` trait for converting to strings
+The `ToString` trait for converting to strings
 
 */
 
+#![experimental]
+
 use fmt;
+use string::String;
 
 /// A generic trait for converting a value to a string
-pub trait ToStr {
+pub trait ToString {
     /// Converts the value of `self` to an owned string
-    fn to_str(&self) -> ~str;
+    fn to_string(&self) -> String;
 }
 
 /// Trait for converting a type to a string, consuming it in the process.
 pub trait IntoStr {
     /// Consume and convert to a string.
-    fn into_str(self) -> ~str;
+    fn into_string(self) -> String;
 }
 
-impl<T: fmt::Show> ToStr for T {
-    fn to_str(&self) -> ~str { format!("{}", *self) }
+impl<T: fmt::Show> ToString for T {
+    fn to_string(&self) -> String {
+        format!("{}", *self)
+    }
 }
 
 #[cfg(test)]
 mod tests {
+    use prelude::*;
     use super::*;
-    use str::StrAllocating;
 
     #[test]
     fn test_simple_types() {
-        assert_eq!(1i.to_str(), "1".to_owned());
-        assert_eq!((-1i).to_str(), "-1".to_owned());
-        assert_eq!(200u.to_str(), "200".to_owned());
-        assert_eq!(2u8.to_str(), "2".to_owned());
-        assert_eq!(true.to_str(), "true".to_owned());
-        assert_eq!(false.to_str(), "false".to_owned());
-        assert_eq!(().to_str(), "()".to_owned());
-        assert_eq!(("hi".to_owned()).to_str(), "hi".to_owned());
+        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());
     }
 
     #[test]
     fn test_vectors() {
-        let x: ~[int] = box [];
-        assert_eq!(x.to_str(), "[]".to_owned());
-        assert_eq!((box [1]).to_str(), "[1]".to_owned());
-        assert_eq!((box [1, 2, 3]).to_str(), "[1, 2, 3]".to_owned());
-        assert!((box [box [], box [1], box [1, 1]]).to_str() ==
-               "[[], [1], [1, 1]]".to_owned());
+        let x: Vec<int> = vec![];
+        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() ==
+               "[[], [1], [1, 1]]".to_string());
     }
 }