From 1d0378c454de72ddcfc08bcc105744923ef2d4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 18 Jun 2020 01:36:43 +0200 Subject: [PATCH 1/2] impl From for String This allows us to write fn char_to_string() -> String { 'a'.into() } which was not possible before. --- src/liballoc/string.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 0378ff5362a8b..cf86ba055b8af 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -2508,3 +2508,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 for String { + #[inline] + fn from(c: char) -> Self { + c.to_string() + } +} From 2cde4932c7e8bd6000378af41029299ccf6eea69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 18 Jun 2020 11:19:03 +0200 Subject: [PATCH 2/2] add test for char into string --- src/liballoc/tests/string.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/liballoc/tests/string.rs b/src/liballoc/tests/string.rs index 9ea020d2d19f4..d38655af78cb7 100644 --- a/src/liballoc/tests/string.rs +++ b/src/liballoc/tests/string.rs @@ -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()); +}