diff options
| author | bors <bors@rust-lang.org> | 2016-08-01 20:05:57 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-01 20:05:57 -0700 |
| commit | 19765f2ab18ebb79f8615951b7cb8b86d896ecda (patch) | |
| tree | 051c2c14daeabe5c148e733d57efa206953d57d5 | |
| parent | 1ece9ca968ab121082481b860e00c470182f0bae (diff) | |
| parent | ac73335f2f5421c914fa3900567696cc6dc73d8d (diff) | |
| download | rust-19765f2ab18ebb79f8615951b7cb8b86d896ecda.tar.gz rust-19765f2ab18ebb79f8615951b7cb8b86d896ecda.zip | |
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.
| -rw-r--r-- | src/libcollections/string.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index f91d8a5f4e1..06952253ef3 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -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] |
