Skip to content

Commit 19765f2

Browse files
authored
Auto merge of #35054 - pwoolcoc:stringfromchars, r=brson
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.
2 parents 1ece9ca + ac73335 commit 19765f2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcollections/string.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,26 @@ impl Into<Vec<u8>> for String {
18811881
}
18821882
}
18831883

1884+
#[stable(feature = "stringfromchars", since = "1.12.0")]
1885+
impl<'a> From<&'a [char]> for String {
1886+
#[inline]
1887+
fn from(v: &'a [char]) -> String {
1888+
let mut s = String::with_capacity(v.len());
1889+
for c in v {
1890+
s.push(*c);
1891+
}
1892+
s
1893+
}
1894+
}
1895+
1896+
#[stable(feature = "stringfromchars", since = "1.12.0")]
1897+
impl From<Vec<char>> for String {
1898+
#[inline]
1899+
fn from(v: Vec<char>) -> String {
1900+
String::from(v.as_slice())
1901+
}
1902+
}
1903+
18841904
#[stable(feature = "rust1", since = "1.0.0")]
18851905
impl fmt::Write for String {
18861906
#[inline]

0 commit comments

Comments
 (0)