]> git.lizzy.rs Git - rust.git/commitdiff
implement `From<Vec<char>>` and `From<&'a [char]>` for `String`
authorPaul Woolcock <paul@woolcock.us>
Tue, 26 Jul 2016 19:54:55 +0000 (15:54 -0400)
committerPaul Woolcock <paul@woolcock.us>
Wed, 27 Jul 2016 16:07:49 +0000 (12:07 -0400)
Though there are ways to convert a slice or vec of chars into a string,
it would be nice to be able to just do `String::from(['a', 'b', 'c'])`,
so this PR implements `From<Vec<char>>` and `From<&'a [char]>` for
String.

src/libcollections/string.rs

index f91d8a5f4e1e84d506fd4562fa69f27d87b4c72d..06952253ef3b09540017a1211dccd8e75b21bfe5 100644 (file)
@@ -1881,6 +1881,26 @@ fn into(self) -> Vec<u8> {
     }
 }
 
+#[stable(feature = "stringfromchars", since = "1.12.0")]
+impl<'a> From<&'a [char]> for String {
+    #[inline]
+    fn from(v: &'a [char]) -> String {
+        let mut s = String::with_capacity(v.len());
+        for c in v {
+            s.push(*c);
+        }
+        s
+    }
+}
+
+#[stable(feature = "stringfromchars", since = "1.12.0")]
+impl From<Vec<char>> for String {
+    #[inline]
+    fn from(v: Vec<char>) -> String {
+        String::from(v.as_slice())
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl fmt::Write for String {
     #[inline]