Skip to content

Commit 245206d

Browse files
committed
Add boxed_str.as_str()
1 parent 697d953 commit 245206d

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

library/alloc/src/boxed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,15 @@ impl<T, A: Allocator> Box<T, A> {
647647
}
648648
}
649649

650+
impl<A: Allocator> Box<str, A> {
651+
#[stable(feature = "box_as_str", since = "CURRENT_RUSTC_VERSION")]
652+
#[inline]
653+
/// Extracts a string slice containing the entire `Box<str>`.
654+
pub fn as_str(&self) -> &str {
655+
&*self
656+
}
657+
}
658+
650659
impl<T> Box<[T]> {
651660
/// Constructs a new boxed slice with uninitialized contents.
652661
///

library/alloc/src/rc/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ fn test_from_box_str() {
448448
use std::string::String;
449449

450450
let s = String::from("foo").into_boxed_str();
451+
assert_eq!((&&&s).as_str(), "foo");
451452
let r: Rc<str> = Rc::from(s);
452453

453454
assert_eq!(&r[..], "foo");

0 commit comments

Comments
 (0)