Skip to content

Rust should suggest single quotes when expecting char #109586

Closed

Description

@SergioB-dev

Code

fn convert_c_to_str(c: char) -> &str {
    match c {
        "A" => "T",
        "C" => "G",
        "T" => "A",
        "G" => "C",
        _ => c,
    }
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
3 |     match c {
  |           - this expression has type `char`
4 |         "A" => "T",
  |         ^^^ expected `char`, found `&str`

error[E0308]: mismatched types
 --> src/main.rs:5:9
  |
3 |     match c {
  |           - this expression has type `char`
4 |         "A" => "T",
5 |         "C" => "G",
  |         ^^^ expected `char`, found `&str`

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
3 |     match c {
  |           - this expression has type `char`
...
6 |         "T" => "A",
  |         ^^^ expected `char`, found `&str`

error[E0308]: mismatched types
 --> src/main.rs:7:9
  |
3 |     match c {
  |           - this expression has type `char`
...

Desired output

...

"Please use single quotes ('_') to generate chars"

Rationale and extra context

For beginners the error message could be a lot simpler. Sometimes they just want to make a char and using single quotes instead of double quotes is not terribly obvious.

Other cases

No response

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 25, 2023
  2. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    on Mar 25, 2023
  3. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Mar 25, 2023
  4. clubby789 commented on Mar 26, 2023

    @clubby789
    Contributor

    and as far as I know, all "mainstream" languages use a single quote to denote a character

    Both JavaScript and Python have no character type, with " and ' being roughly interchangeable, so this is likely to be a common mistake for newcomers from other languages. Either way, whether it's a typo or a misunderstanding, having an IDE-applicable suggestion will help.

  5. added a commit that references this issue on Mar 27, 2023
  6. added a commit that references this issue on Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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