Skip to content

"only u8 can be cast as char, not i32" needs further information #91836

Closed
@GKFX

Description

@GKFX

Given the following code: link

0x61i32 as char

The current output is:

only `u8` can be cast as `char`, not `i32`

This is not very clear. There are two pieces of background information the user needs here:

  • chars are Unicode scalar values, which are any integer value from 0 to 0xD7FF and 0xE000 to 0x10FFFF. Only u8 can be statically guaranteed to meet this requirement so only u8 can be cast to char.
  • char::from_u32 is probably what the user wants, if they are interested in non-ASCII values.

The user is directed to further information available at rustc --explain E0604 and https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions, neither of which explains either of those two points.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.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