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

Make core::str::from_utf8* methods available on the str type #398

Closed
robertbastian opened this issue Jun 22, 2024 · 1 comment
Closed

Make core::str::from_utf8* methods available on the str type #398

robertbastian opened this issue Jun 22, 2024 · 1 comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@robertbastian
Copy link

robertbastian commented Jun 22, 2024

Proposal

Problem statement

These are constructors for str, but they live in the core::str module. This means users always need a use core::str import in order to call them as str::from_utf8, or have to fully qualify them. This leads to two ways of doing things, and is also confusing for beginners who might think that core::str imports the type and is always needed when using str.

Motivating examples or use cases

pub use core::str;

fn foo1() -> &'static str {
    str::from_utf8(b"hi").unwrap()
}
pub use core::str; # not needed, even though we use `str`

fn foo2() -> &'static str {
    "hi"
}
fn foo3() -> &'static str {
    core::str::from_utf8(b"hi").unwrap()
}

Solution sketch

  • from_utf8
  • from_utf8_mut
  • from_utf8_unchecked
  • from_utf8_unchecked_mut

become associated functions on the str type. The core::str module reexports them, with a future deprecation warning, as was done for core::i16::MAX and friends.

Alternatives

Everything stays as it is

Links and related work

Open questions

  • There are two str_internals functions, next_code_point and utf8_char_width, not sure whether to include those.
@robertbastian robertbastian added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jun 22, 2024
@robertbastian robertbastian changed the title Make core::str::from_utf* methods available on the str type Make core::str::from_utf8* methods available on the str type Jun 22, 2024
@joshtriplett joshtriplett added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Oct 1, 2024
@joshtriplett
Copy link
Member

Looks good! Ship it. 🚢

The reaction in today's @rust-lang/libs-api meeting was "why didn't we do this years ago".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants