Skip to content

Mark core::str::from_utf8* as deprecated #655

@sorairolake

Description

@sorairolake

The following methods were stabilized in Rust 1.87.0:

The only difference between these methods and core::str::from_utf8* such as core::str::from_utf8 is the namespace (str primitive type or core::str module). I don't think there's any reason to use the traditional core::str::from_utf8* instead of <str>::from_utf8* if the MSRV is 1.87.0.

So I propose adding the deprecated attribute to core::str::from_utf8* as follows:

// `library/core/src/str/converts.rs`
#[deprecated(since = "1.87.0", note = "use `<str>::from_utf8` instead")]
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
    ...
}

Replacing core::str::from_utf8* with <str>::from_utf8* is as simple as:

// some bytes, in a vector
let sparkle_heart = vec![240, 159, 146, 150];

// We can use the ? (try) operator to check if the bytes are valid
-let sparkle_heart = core::str::from_utf8(&sparkle_heart)?;
+let sparkle_heart = str::from_utf8(&sparkle_heart)?;

assert_eq!("💖", sparkle_heart);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions