diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 14774e2d2293c..fe638fd641773 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -78,6 +78,7 @@ - [fmt_internals](fmt-internals.md) - [fn_traits](fn-traits.md) - [fnbox](fnbox.md) +- [from_utf8_error_as_bytes](from_utf8_error_as_bytes.md) - [fundamental](fundamental.md) - [fused](fused.md) - [future_atomic_orderings](future-atomic-orderings.md) diff --git a/src/doc/unstable-book/src/from_utf8_error_as_bytes.md b/src/doc/unstable-book/src/from_utf8_error_as_bytes.md new file mode 100644 index 0000000000000..570f779417f09 --- /dev/null +++ b/src/doc/unstable-book/src/from_utf8_error_as_bytes.md @@ -0,0 +1,7 @@ +# `from_utf8_error_as_bytes` + +The tracking issue for this feature is: [#40895] + +[#40895]: https://github.com/rust-lang/rust/issues/40895 + +------------------------ diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 7d9d7276201bd..8d6cf30511260 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1403,6 +1403,26 @@ impl String { } impl FromUtf8Error { + /// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(from_utf8_error_as_bytes)] + /// // some invalid bytes, in a vector + /// let bytes = vec![0, 159]; + /// + /// let value = String::from_utf8(bytes); + /// + /// assert_eq!(&[0, 159], value.unwrap_err().as_bytes()); + /// ``` + #[unstable(feature = "from_utf8_error_as_bytes", reason = "recently added", issue = "40895")] + pub fn as_bytes(&self) -> &[u8] { + &self.bytes[..] + } + /// Returns the bytes that were attempted to convert to a `String`. /// /// This method is carefully constructed to avoid allocation. It will