From ac73335f2f5421c914fa3900567696cc6dc73d8d Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Tue, 26 Jul 2016 15:54:55 -0400 Subject: [PATCH] implement `From>` and `From<&'a [char]>` for `String` 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>` and `From<&'a [char]>` for String. --- src/libcollections/string.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index f91d8a5f4e1e8..06952253ef3b0 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1881,6 +1881,26 @@ impl Into> for String { } } +#[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> for String { + #[inline] + fn from(v: Vec) -> String { + String::from(v.as_slice()) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Write for String { #[inline]