diff options
| author | Paul Woolcock <paul@woolcock.us> | 2016-07-26 15:54:55 -0400 |
|---|---|---|
| committer | Paul Woolcock <paul@woolcock.us> | 2016-07-27 12:07:49 -0400 |
| commit | ac73335f2f5421c914fa3900567696cc6dc73d8d (patch) | |
| tree | 145e442e5fb9492254a43f46add6ae535a76b9a5 | |
| parent | 422ebd5328e485462c675af9304f3b5b86e5b893 (diff) | |
| download | rust-ac73335f2f5421c914fa3900567696cc6dc73d8d.tar.gz rust-ac73335f2f5421c914fa3900567696cc6dc73d8d.zip | |
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] |
