Skip to content

Rust should suggest single quotes when expecting char #109586

Closed
@SergioB-dev

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

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