Skip to content

Commit

Permalink
Rollup merge of rust-lang#73466 - matthiaskrgr:char_into_string, r=dt…
Browse files Browse the repository at this point in the history
…olnay

impl From<char> for String

This allows us to write

````rust
fn char_to_string() -> String {
    'a'.into()
}
````

which was not possible before.
  • Loading branch information
Manishearth authored Jul 1, 2020
2 parents b8eabed + 2cde493 commit 6ef6fa6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2518,3 +2518,11 @@ impl DoubleEndedIterator for Drain<'_> {

#[stable(feature = "fused", since = "1.26.0")]
impl FusedIterator for Drain<'_> {}

#[stable(feature = "from_char_for_string", since = "1.46.0")]
impl From<char> for String {
#[inline]
fn from(c: char) -> Self {
c.to_string()
}
}
7 changes: 7 additions & 0 deletions src/liballoc/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,10 @@ fn test_try_reserve_exact() {
}
}
}

#[test]
fn test_from_char() {
assert_eq!(String::from('a'), 'a'.to_string());
let s: String = 'x'.into();
assert_eq!(s, 'x'.to_string());
}

0 comments on commit 6ef6fa6

Please sign in to comment.