Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a panic example to std::from_utf8_unchecked #35890

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/libcore/str/mod.rs
Expand Up @@ -213,9 +213,9 @@ impl Utf8Error {
/// use std::str;
///
/// // some invalid bytes, in a vector
/// let sparkle_heart = vec![0, 159, 146, 150];
/// let invalid_bytes = vec![0, 159, 146, 150];
///
/// assert!(str::from_utf8(&sparkle_heart).is_err());
/// assert!(str::from_utf8(&invalid_bytes).is_err());
/// ```
///
/// See the docs for [`Utf8Error`][error] for more details on the kinds of
Expand Down Expand Up @@ -299,6 +299,21 @@ unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str {
///
/// assert_eq!("💖", sparkle_heart);
/// ```
///
/// Incorrect bytes:
///
/// ```no_run
/// use std::str;
///
/// // some invalid bytes, in a vector
/// let invalid_bytes = vec![0, 159, 146, 150];
///
/// unsafe {
/// let invalid_str = str::from_utf8_unchecked(&invalid_bytes);
/// // prints unknown garbage to stdout
/// println!("{}", invalid_str);
/// }
/// ```
#[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
Expand Down