Skip to content

Poor error when passing "string" instead of b"string" #44307

Closed
@clarfonthey

Description

@clarfonthey

Playground: https://play.rust-lang.org/?gist=935daee28b05c2afcdf61423fdfb40f8&version=stable

fn parse_bytes(s: &[u8]) {
    // do something
}

fn main() {
    parse_bytes("hello");
}

This includes the error:

error[E0308]: mismatched types
 --> src/main.rs:6:17
  |
6 |     parse_bytes("hello");
  |                 ^^^^^^^ expected slice, found str
  |
  = note: expected type `&[u8]`
             found type `&'static str`
  = help: here are some functions which might fulfill your needs:
          - .as_bytes()

Whereas it could be much better suggesting the user to type b"hello" instead of supplying "hello".as_bytes().

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions