diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 36641284a769b..76235a3641d12 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -147,8 +147,10 @@ use core::slice;
use core::task::{Context, Poll};
use crate::alloc::{self, AllocRef, Global};
+use crate::borrow::Cow;
use crate::raw_vec::RawVec;
use crate::str::from_boxed_utf8_unchecked;
+use crate::string::String;
use crate::vec::Vec;
/// A pointer type for heap allocation.
@@ -1045,6 +1047,41 @@ impl FromIterator for Box<[A]> {
}
}
+#[stable(feature = "boxed_str_from_iter", since = "1.44.0")]
+impl<'a> FromIterator<&'a char> for Box {
+ fn from_iter>(iter: T) -> Self {
+ iter.into_iter().collect::().into_boxed_str()
+ }
+}
+
+#[stable(feature = "boxed_str_from_iter", since = "1.44.0")]
+impl<'a> FromIterator<&'a str> for Box {
+ fn from_iter>(iter: T) -> Self {
+ iter.into_iter().collect::().into_boxed_str()
+ }
+}
+
+#[stable(feature = "boxed_str_from_iter", since = "1.44.0")]
+impl<'a> FromIterator> for Box {
+ fn from_iter>>(iter: T) -> Self {
+ iter.into_iter().collect::().into_boxed_str()
+ }
+}
+
+#[stable(feature = "boxed_str_from_iter", since = "1.44.0")]
+impl FromIterator for Box {
+ fn from_iter>(iter: T) -> Self {
+ iter.into_iter().collect::().into_boxed_str()
+ }
+}
+
+#[stable(feature = "boxed_str_from_iter", since = "1.44.0")]
+impl FromIterator for Box {
+ fn from_iter>(iter: T) -> Self {
+ iter.into_iter().collect::().into_boxed_str()
+ }
+}
+
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl Clone for Box<[T]> {
fn clone(&self) -> Self {
diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs
index 1b6e0bb291c35..99f012543e603 100644
--- a/src/liballoc/tests.rs
+++ b/src/liballoc/tests.rs
@@ -8,7 +8,9 @@ use core::i64;
use core::ops::Deref;
use core::result::Result::{Err, Ok};
+use std::borrow::Cow;
use std::boxed::Box;
+use std::string::String;
#[test]
fn test_owned_clone() {
@@ -140,6 +142,22 @@ fn boxed_slice_from_iter() {
assert_eq!(boxed[7], 7);
}
+#[test]
+fn boxed_str_from_iter() {
+ let s = "Hello, world!";
+ let chars: Box<[char]> = s.chars().collect();
+ let boxed: Box = chars.iter().collect();
+ assert_eq!(&*boxed, s);
+ let boxed: Box = [s].iter().cloned().collect();
+ assert_eq!(&*boxed, s);
+ let boxed: Box = [Cow::from(s)].iter().cloned().collect();
+ assert_eq!(&*boxed, s);
+ let boxed: Box = [String::from(s)].iter().cloned().collect();
+ assert_eq!(&*boxed, s);
+ let boxed: Box = s.chars().collect();
+ assert_eq!(&*boxed, s);
+}
+
#[test]
fn test_array_from_slice() {
let v = vec![1, 2, 3];