Repository navigation
Rust should suggest single quotes when expecting char #109586
Copy link
Copy link
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: 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.Call 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: 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.Call 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
clubby789 commentedon Mar 26, 2023
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.Rollup merge of rust-lang#109613 - lukas-code:match-str-to-char-sugge…
Rollup merge of rust-lang#109613 - lukas-code:match-str-to-char-sugge…