Skip to content

str primitive usage should automatically recommend str module functions #46660

Closed
@mqudsi

Description

@mqudsi

Using str without the std::str prefix to specify the module generates unhelpful error messages (especially since the distinction between the std::str module and the str primitive is not very nicely thought out due to technical restrictions I wish we could revisit).

Example code:

fn main() {
    let bytes = "hello world".as_bytes();
    let string = unsafe {
        str::from_utf8(bytes)
    };

    println!("{}", string);
}

This causes the compiler to emit the following error:

error[E0599]: no function or associated item named `from_utf8` found for type `str` in the current scope
 --> ./test.rs:4:9
  |
4 |         str::from_utf8(bytes)
  |         ^^^^^^^^^^^^^^

error: aborting due to previous error

A recommendation to specify std:: to access the module function with the same name would be most helpful. In fact, anything the compiler could do to blur the distinction between the type and the primitive would be most appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions