]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/str.rs
auto merge of #15271 : jasonthompson/rust/docs/str, r=huonw
[rust.git] / src / libcollections / str.rs
index 024e8f0f8eeea3c87942c210c5d49e772d9c5406..7e47f02d21a0ce043546bd51281c19b03026fec6 100644 (file)
@@ -811,15 +811,9 @@ fn to_owned(&self) -> String {
     }
 
     /// Converts to a vector of `u16` encoded as UTF-16.
+    #[deprecated = "use `utf16_units` instead"]
     fn to_utf16(&self) -> Vec<u16> {
-        let me = self.as_slice();
-        let mut u = Vec::new();
-        for ch in me.chars() {
-            let mut buf = [0u16, ..2];
-            let n = ch.encode_utf16(buf /* as mut slice! */);
-            u.push_all(buf.slice_to(n));
-        }
-        u
+        self.as_slice().utf16_units().collect::<Vec<u16>>()
     }
 
     /// Given a string, make a new string with repeated copies of it.
@@ -1627,14 +1621,17 @@ fn test_utf16() {
 
         for p in pairs.iter() {
             let (s, u) = (*p).clone();
+            let s_as_utf16 = s.as_slice().utf16_units().collect::<Vec<u16>>();
+            let u_as_string = from_utf16(u.as_slice()).unwrap();
+
             assert!(is_utf16(u.as_slice()));
-            assert_eq!(s.to_utf16(), u);
+            assert_eq!(s_as_utf16, u);
 
-            assert_eq!(from_utf16(u.as_slice()).unwrap(), s);
+            assert_eq!(u_as_string, s);
             assert_eq!(from_utf16_lossy(u.as_slice()), s);
 
-            assert_eq!(from_utf16(s.to_utf16().as_slice()).unwrap(), s);
-            assert_eq!(from_utf16(u.as_slice()).unwrap().to_utf16(), u);
+            assert_eq!(from_utf16(s_as_utf16.as_slice()).unwrap(), s);
+            assert_eq!(u_as_string.as_slice().utf16_units().collect::<Vec<u16>>(), u);
         }
     }