Skip to content

Commit

Permalink
Auto merge of #35054 - pwoolcoc:stringfromchars, r=brson
Browse files Browse the repository at this point in the history
implement `From<Vec<char>>` 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<Vec<char>>` and `From<&'a [char]>` for
String.
  • Loading branch information
bors authored Aug 2, 2016
2 parents 1ece9ca + ac73335 commit 19765f2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,26 @@ impl Into<Vec<u8>> 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<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]
Expand Down

0 comments on commit 19765f2

Please sign in to comment.