提交 ac73335f 编写于 作者: P Paul Woolcock

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.
上级 422ebd53
......@@ -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]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册